2017-04-17 105 views
0

我试图捕获图像并在imageView中设置,就像在此代码中提供了某人在计算器中,但我得到空白的白色屏幕。 可能是什么问题? 谢谢。尝试捕获图像并在imageView中设置时空白白色屏幕

private String pictureImagePath = ""; 
private void openBackCamera() { 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
    String imageFileName = timeStamp + ".jpg"; 
    File storageDir = Environment.getExternalStoragePublicDirectory(
      Environment.DIRECTORY_PICTURES); 
    pictureImagePath = storageDir.getAbsolutePath() + "/" + imageFileName; 
    File file = new File(pictureImagePath); 
    Uri outputFileUri = Uri.fromFile(file); 
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);    
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); 
    startActivityForResult(cameraIntent, 1); 
} 

地处理图像

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == 1) { 
    File imgFile = new File(pictureImagePath); 
     if(imgFile.exists()){   
     Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); 
     ImageView myImage = (ImageView) findViewById(R.id.imageviewTest); 
     myImage.setImageBitmap(myBitmap); 

     } 
    } 

} 
+0

你在清单文件中添加的权限? –

+0

[检查我的答案在下面的链接](http://stackoverflow.com/questions/43450296/how-to-get-save-an-image-at-location-and-retrieve-it-for-imageview-in- android-ap/43450770#43450770) –

+0

也许位图太大,你的设备没有足够的内存?检查你的日志BitmapToLargeException –

回答

0

下面是我用得到位图的一个的ImageView中显示的代码(对不起它在MonoDroid的/ C#,这将需要光修饰的Android/Java的工作):

using System.IO; 
using Android.Graphics; 
using Android.Graphics.Drawables; 
using Android.Widget; 

namespace MyApp.Util 
{ 
    public static class BitmapHelpers 
    { 
     /// <summary> 
     /// This method will recyle the memory help by a bitmap in an ImageView 
     /// </summary> 
     /// <param name="imageView">Image view.</param> 
     public static void RecycleBitmap(this ImageView imageView) 
     { 
      if (imageView == null) { 
       return; 
      } 

      Drawable toRecycle = imageView.Drawable; 
      if (toRecycle != null) { 
       ((BitmapDrawable)toRecycle).Bitmap.Recycle(); 
      } 
     } 

     public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) 
     { 
      // Raw height and width of image 
      int height = options.OutHeight; 
      int width = options.OutWidth; 
      int inSampleSize = 1; 

      if (height > reqHeight || width > reqWidth) 
      { 
       int halfHeight = height/2; 
       int halfWidth = width/2; 

       // Calculate the largest inSampleSize value that is a power of 2 and keeps both 
       // height and width larger than the requested height and width. 
       while ((halfHeight/inSampleSize) > reqHeight 
         && (halfWidth/inSampleSize) > reqWidth) { 
        inSampleSize *= 2; 
       } 
      } 

      return inSampleSize; 
     } 

     /// <summary> 
     /// Load the image from the device, and resize it to the specified dimensions. 
     /// </summary> 
     /// <returns>The and resize bitmap.</returns> 
     /// <param name="fileName">File name.</param> 
     /// <param name="width">Width.</param> 
     /// <param name="height">Height.</param> 
     public static Bitmap LoadAndResizeBitmap(this string fileName, int width, int height) 
     { 
      // First we get the the dimensions of the file on disk 
      BitmapFactory.Options options = new BitmapFactory.Options 
      { 
       InPurgeable = true, 
       InJustDecodeBounds = true 
      };      

      BitmapFactory.DecodeFile(fileName, options); 
      options.InSampleSize = calculateInSampleSize(options, width, height); 
      options.InJustDecodeBounds = false; 
      Bitmap resizedBitmap = BitmapFactory.DecodeFile(fileName, options); 

      return resizedBitmap; 
     } 
    } 
} 

示例用法

public static Bitmap LoadBitmap(File imageFile, int w, int h) 
{ 
    Bitmap bitmap = null; 

    if (imageFile != null) 
    { 
     if((new File(imageFile.Path)).Exists()) 
     { 
      bitmap = imageFile.Path.LoadAndResizeBitmap(w, h); 
     } 
    } 

    return bitmap; 
} 

OnActivityResult片段(使在库中提供的图片)

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data) 
{ 
    base.OnActivityResult(requestCode, resultCode, data);   

    switch ((ActivityRequestCode)requestCode) 
    { 
     case ActivityRequestCode.Camera: 
      // make it available in the gallery 
      if (_imageFile != null) 
      { 
       Intent mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile); 
       Uri contentUri = Uri.FromFile(_imageFile); 
       mediaScanIntent.SetData(contentUri); 
       SendBroadcast(mediaScanIntent); 

       _pictureRequestingFragment.PictureReady(_imageFile); 
      } 

      break; 

     case ActivityRequestCode.Map: 
      break; 
    } 
}