旋转设备时出现OutOfMemory错误

时间:2012-12-24 05:50:11

标签: android rotation imageview out-of-memory

我使用TouchImageView创建了一个图像查看器项目,以实现缩放,拖动和拖动,全景查看器等。

查看器工作正常,但旋转设备时出现OutOfMemory错误,特别是全景图(不是很大,3.5 MB应该很容易通过Galaxy Note处理)。这是违规行:

final Bitmap myBitmap = BitmapFactory.decodeFile(filename);

当然,因为它是一个全景图,我想要缩放,所以在解码时压缩图像,如建议的here,不是一个选项(我已经尝试过了,但有效,但是低质量杀死了全景的想法。

我不认为我的项目中存在内存泄漏,因为它不是内存密集型,只是在旋转时冻结,而且违规行仅在旋转时执行一次。我可以看到全景在屏幕上对角冻结。我认为问题是ImageView的“旋转动画”。也许我应该禁用它?如果是这样,怎么样?

以下是崩溃日志。

谢谢!

LOG -

58:38.845: W/dalvikvm(2465): threadid=1: thread exiting with uncaught exception (group=0x40c531f8)
12-24 02:58:38.850: E/AndroidRuntime(2465): FATAL EXCEPTION: main
12-24 02:58:38.850: E/AndroidRuntime(2465): java.lang.OutOfMemoryError
12-24 02:58:38.850: E/AndroidRuntime(2465):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
12-24 02:58:38.850: E/AndroidRuntime(2465):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
12-24 02:58:38.850: E/AndroidRuntime(2465):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:389)
12-24 02:58:38.850: E/AndroidRuntime(2465):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:418)
12-24 02:58:38.850: E/AndroidRuntime(2465):     at com.floritfoto.apps.xvf.Foto.pic(Foto.java:180)
12-24 02:58:38.850: E/AndroidRuntime(2465):     at com.floritfoto.apps.xvf.Foto.onCreate(Foto.java:383)
12-24 02:58:38.850: E/AndroidRuntime(2465):     at android.app.Activity.performCreate(Activity.java:4465)
12-24 02:58:38.850: E/AndroidRuntime(2465):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
12-24 02:58:38.850: E/AndroidRuntime(2465):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
12-24 02:58:38.850: E/AndroidRuntime(2465):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
12-24 02:58:38.850: E/AndroidRuntime(2465):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3363)
12-24 02:58:38.850: E/AndroidRuntime(2465):     at android.app.ActivityThread.access$700(ActivityThread.java:127)
12-24 02:58:38.850: E/AndroidRuntime(2465):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1163)
12-24 02:58:38.850: E/AndroidRuntime(2465):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-24 02:58:38.850: E/AndroidRuntime(2465):     at android.os.Looper.loop(Looper.java:137)
12-24 02:58:38.850: E/AndroidRuntime(2465):     at android.app.ActivityThread.main(ActivityThread.java:4507)
12-24 02:58:38.850: E/AndroidRuntime(2465):     at java.lang.reflect.Method.invokeNative(Native Method)
12-24 02:58:38.850: E/AndroidRuntime(2465):     at java.lang.reflect.Method.invoke(Method.java:511)
12-24 02:58:38.850: E/AndroidRuntime(2465):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
12-24 02:58:38.850: E/AndroidRuntime(2465):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
12-24 02:58:38.850: E/AndroidRuntime(2465):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

首先尝试调试代码并检查堆大小如何增加,然后查看以下Link

了解android如何计算图像大小及其尺寸。  要缩小图像,您可以尝试下面的代码 如果你想按比例缩小

public static Bitmap getResizedBitmap(Bitmap image, int newHeight, int newWidth) {
    int width = image.getWidth();
    int height = image.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // create a matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);
    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(image, 0, 0, width, height,
            matrix, false);
    return resizedBitmap;
}

如果您想手动缩小尺寸,您可以将图像的尺寸更改为其原始尺寸的一半

答案 1 :(得分:-2)

尝试使用此功能..

public static Bitmap decodeFile(File f,int WIDTH,int HIGHT){
        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_WIDTH=WIDTH;
            final int REQUIRED_HIGHT=HIGHT;
            //Find the correct scale value. It should be the power of 2.
            int scale=1;
            while(o.outWidth/scale/2>=REQUIRED_WIDTH && o.outHeight/scale/2>=REQUIRED_HIGHT)
                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;
    }