0

我制作了一个图像查看器项目,TouchImageView用于实现缩放、拖放、全景查看器等。

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

final Bitmap myBitmap = BitmapFactory.decodeFile(filename);

当然,因为它是全景图,所以我想缩放,所以在解码时压缩图像,如此处所建议,不是一种选择(我已经尝试过,并且有效,但是低质量扼杀了全景图的想法)。

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

下面是崩溃日志。

谢谢!!

日志-

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

2 回答 2

0

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

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

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

于 2012-12-24T06:08:28.063 回答
-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;
    }
于 2012-12-24T05:54:28.073 回答