【问题标题】:Blank white screen when trying to capture an image and set in imageView尝试捕获图像并在 imageView 中设置时出现空白白屏
【发布时间】:2017-04-17 12:39:06
【问题描述】:

我正在尝试捕获图像并在 imageView 中设置,就像 stackoverflow 中有人提供的这段代码一样,但我得到空白屏幕。 可能是什么问题呢? 谢谢。

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);

        }
    }

}

【问题讨论】:

  • 您是否在清单文件中添加了权限?
  • 可能是位图太大而您的设备没有足够的内存?检查您的日志中的 BitmapToLargeException
  • 您在哪个 Android 版本上进行测试?这不适用于牛轧糖及更高版本。
  • @Arunkumar,您的代码给了我错误:位图太大而无法上传到纹理中。当我尝试使用 Bitmap.createScaledBitmap 缩放位图时,图片变为横向模式。

标签: java android image-capture


【解决方案1】:

这是我用来在 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 Snippet(使图片在图库中可用)

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;
    }
}

【讨论】:

    猜你喜欢
    • 2012-08-18
    • 2011-12-06
    • 2019-11-19
    • 1970-01-01
    • 2015-06-21
    • 1970-01-01
    • 2023-01-05
    • 2016-08-06
    • 2011-03-15
    相关资源
    最近更新 更多