在某些设备上,从Android拍照时会出现OutOfMemoryError错误。

3

当我使用某些设备和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());
                }

            }

}

我希望有人可以帮助我……我不知道该怎么做……
提前致谢。
敬礼。
2个回答

4

看起来你只想显示一个160x160的位图,但是在这行代码中你读取了整个位图,然后在后面把它缩小。

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

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

以下是来自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);
}

然后,要获取您的160x160图像,您只需要执行以下操作:
ivImageLoad.setImageBitmap(decodeSampledBitmapFromUri(selectedImage, 100, 100));

0

问题的根源可能是默认堆大小。有方法可以增加它:

对于Android 2.2或更低版本:

dalvik.system.VMRuntime.getRuntime().setMinimumHeapSize(sizeInBytes);

针对 Android 2.3 或更高版本

android:largeHeap="true"

检查问题的源头是否在这里,相应地增加堆大小。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接