5

我的问题

我用我的安卓设备拍照。然后我从文件中解码该图片。

        Bitmap photo = BitmapFactory.decodeFile(EXTERNAL_IMAGE_PATH+File.separator+this._currentPhotoName+JPEG_FILE_SUFFIX);
        if (photo == null && data != null) 
            photo = (Bitmap) data.getExtras().get("data");
        else if (data == null && photo == null)
            Log.e("CCPhotoManager","Can't find image from file or from intent data.");

然后我检查那张图片,看看它是否需要旋转到正确的方向。

             try {
                ExifInterface exif = new ExifInterface(EXTERNAL_IMAGE_PATH+File.separator+this._currentPhotoName+JPEG_FILE_SUFFIX);
                int rotation = CCDataUtils.exifToDegrees(exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL));
                Log.v("CCPhotoManager", "Rotation:"+rotation);
                if (rotation > 0) {
                    photo = this.convertSavedImageToCorrectOrientation(EXTERNAL_IMAGE_PATH+File.separator+this._currentPhotoName+JPEG_FILE_SUFFIX, photo, rotation);
                }
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

如果它确实需要旋转,我称之为这个方法。

public Bitmap convertSavedImageToCorrectOrientation(String filePath,Bitmap photo,int rotation) {
        Log.d("CCPhotoManager", "Changing Orientation of photo located at: "+filePath+" Rotating by:"+rotation);
        int width = photo.getWidth();
        int height = photo.getHeight();


        Matrix matrix = new Matrix();
        matrix.preRotate(rotation);

        Bitmap adjusted = Bitmap.createBitmap(photo, 0, 0, width, height, matrix, true);

        try {
               FileOutputStream out = new FileOutputStream(filePath);
               adjusted.compress(Bitmap.CompressFormat.JPEG, 100, out);
        } catch (Exception e) {
               e.printStackTrace();
        }

        return adjusted;
    }

如果在线调用,我会收到内存不足的投诉convertSavedImageToCorrectOrientationBitmap adjusted = Bitmap.createBitmap(photo,0,0,width,height,matrix,true);

这只是在Samsung Galaxy S3. 它在和Samsung Galaxy Ace上运行良好。HTC HeroSony Xperia U

这是错误。

10-17 14:33:33.950: E/AndroidRuntime(12556): java.lang.OutOfMemoryError
10-17 14:33:33.950: E/AndroidRuntime(12556):    at android.graphics.Bitmap.nativeCreate(Native Method)
10-17 14:33:33.950: E/AndroidRuntime(12556):    at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
10-17 14:33:33.950: E/AndroidRuntime(12556):    at android.graphics.Bitmap.createBitmap(Bitmap.java:551)

这也是一个巨大的内存量。

10-17 14:33:33.945: E/dalvikvm-heap(12556): Out of memory on a 31961104-byte allocation.

我认为这与周围的位图数量有关,但我不确定如何阻止此错误的发生。

我知道你可以打电话.recycle();给他们,但它似乎不起作用。

我的问题

如何正确处理我的位图,这样我就没有这个 OOM 问题?

提前致谢

4

3 回答 3

4

我有完全相同的问题,在完全相同的设备上做完全相同的事情!不幸的是,在我的情况下,需要使用完整尺寸的原始图像将图像提交给网络服务。对我有用的是在清单的应用程序元素中打开largeHeap

这不会永久解决问题 - 设备可能会配备更大的相机,即使启用了 largeHeap,图像也无法放入内存。为了捕捉这种极端情况,我还在旋转图像的代码周围放置了一个 try catch,并向用户显示了一个很好的错误。

一个更完整的解决方案是编写您自己的 jpeg 操作代码,该代码可以使用基于流的方法旋转 jpeg,以便永远不需要将图像加载到内存中。

于 2013-02-07T01:17:38.710 回答
4

对于内存不足的问题

//解码图像并对其进行缩放以减少内存消耗

private Bitmap decodeFile(File f){

try {
    //Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(new FileInputStream(f),null,o);

    //The new size we want to scale to
    final int REQUIRED_SIZE=70;

    //Find the correct scale value. It should be the power of 2.
    int scale=1;
    while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)
        scale*=2;

    //Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize=scale;
    return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}
于 2012-10-17T13:50:22.603 回答
3

以下是如何调整大小/旋转的更完整示例,部分摘自 Android 开发人员指南(更改 REQ_WIDTH 和 REQ_HEIGHT):

private static final int REQ_WIDTH = 450;
private static final int REQ_HEIGHT = 450;

/**
 * Resize, crop, rotate and Inserts the picture on the layout.
 * 
 * @param mImageView to insert the bitmap.
 * @param imageURI from wich to obtain the bitmap.
 * 
 */
private void setPic(ImageView mImageView, String imageURI) {
    // Get the original bitmap dimensions
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(imageURI, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, REQ_HEIGHT, REQ_WIDTH);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    Bitmap bitmap = BitmapFactory.decodeFile(imageURI, options);

    //need rotation?
    float rotation = rotationForImage(getActivity(), Uri.fromFile(new File(imageURI)));

    if (rotation != 0) {
        //rotate
        Matrix matrix = new Matrix();
        matrix.preRotate(rotation);
        mImageView.setImageBitmap(Bitmap.createBitmap(bitmap, 0, 0, REQ_HEIGHT, REQ_WIDTH, matrix, true));
    } else {
        //use the original
        mImageView.setImageBitmap(BitmapFactory.decodeFile(imageURI, options));
    }
}

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 float rotationForImage(Context context, Uri uri) {
    try {
        if (uri.getScheme().equals("content")) {
            String[] projection = { Images.ImageColumns.ORIENTATION };
            Cursor c = context.getContentResolver().query(uri, projection, null, null, null);
            if (c.moveToFirst()) {
                return c.getInt(0);
            }
        } else if (uri.getScheme().equals("file")) {
            ExifInterface exif = new ExifInterface(uri.getPath());
            int rotation = (int) exifOrientationToDegrees(exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL));
            return rotation;
        }
        return 0;

    } catch (IOException e) {
        Log.e(TAG, "Error checking exif", e);
        return 0;
    }
}

private static float exifOrientationToDegrees(int exifOrientation) {
    if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
        return 90;
    } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
        return 180;
    } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
        return 270;
    }
    return 0;
}
于 2012-10-17T15:41:05.373 回答