ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

在某些设备中拍摄照片android的OutOfMemeryError

2019-10-31 17:26:42  阅读:214  来源: 互联网

标签:camera out-of-memory android


在某些设备和某些版本的Android中用相机拍照时,我遇到问题.例如,在我的Xperia U v4.0.3中,我的代码可以正常工作,但在另一个xperia U v2.3.3中,则无法正常工作…

我读到很多有关此错误的信息,但无法解决…

我拍摄照片并显示的代码:

public void callCamera(){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photo = new File(Environment.getExternalStorageDirectory(),  "Pic.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
startActivityForResult(intent, SELECT_CAMERA);
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == SELECT_CAMERA) {


                Uri selectedImage = imageUri;
                getContentResolver().notifyChange(selectedImage, null);

                ContentResolver cr = getContentResolver();

                try {

                    imageSelected = android.provider.MediaStore.Images.Media
                     .getBitmap(cr, selectedImage);
                    // I tried to read the image created for the camera in the Sd
                    //But I've got the same error... 
                    //imageSelected = Utils.readImageSD(this, selectedImage.getPath());

                     imageSelected = Utils.rotateImage(Utils.scaleBitmap(imageSelected, 160, 160));
                     ivImageLoad.setImageBitmap(imageSelected);

                } catch (Exception e) {
                    Utils.showToast(getApplicationContext(), R.string.errorLoad);
                    Log.e("Camera", e.toString());
                }

            }

}

我希望有人能帮助我…我不知道该怎么办…

提前致谢.

问候.

解决方法:

看起来您只想显示一个160×160的位图,但是在这一行上,您正在读取整个位图,然后将其缩小.

imageSelected = android.provider.MediaStore.Images.Media(cr, selectedImage);

您可以避免使用inJustDecodeBounds将整个位图加载到内存中,然后可以使用inSampleSize对其进行缩放.

这是来自Loading Large Bitmaps Efficiently的Android系列的一些示例代码

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

    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
            inSampleSize = Math.round((float)height / (float)reqHeight);
        } else {
            inSampleSize = Math.round((float)width / (float)reqWidth);
        }
    }
    return inSampleSize;
}

public static Bitmap decodeSampledBitmapFromUri(Uri uri,
        int reqWidth, int reqHeight) {


    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options);
}

然后,获取160×160的图片,您只需执行以下操作:

ivImageLoad.setImageBitmap(decodeSampledBitmapFromUri(selectedImage, 100, 100));

标签:camera,out-of-memory,android
来源: https://codeday.me/bug/20191031/1977390.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有