2017-04-17 65 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; 
    } 
}