0

我正在运行时创建可绘制的位图,并将它们作为我在活动onResume()事件中的视图的背景,并且我正在删除背景图像onStop()事件,以删除位图引用以便可以回收位图。该应用程序适用于 android 版本 2.3.5 但它在 android 版本 4.1.2 上引发异常。可能是什么原因?. 我还使用了来自http://developer.android.com/training/displaying-bitmaps/manage-memory.html的 softReferences 。我收到以下异常。任何帮助将不胜感激。

    10-11 14:54:48.812: E/dalvikvm-heap(1760): Out of memory on a 41508-byte allocation.
    10-11 14:54:48.872: E/AndroidRuntime(1760): FATAL EXCEPTION: main
    10-11 14:54:48.872: E/AndroidRuntime(1760): java.lang.OutOfMemoryError
    10-11 14:54:48.872: E/AndroidRuntime(1760):  at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
    10-11 14:54:48.872: E/AndroidRuntime(1760):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:527)
    10-11 14:54:48.872: E/AndroidRuntime(1760):  at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:301)
4

1 回答 1

1

在选择文件时,使用以下功能将其转换为位图并使用结果,,

public static Bitmap convertToBitmap(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=250;

                //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;
                bmp=BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
                return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
            } catch (FileNotFoundException e) {

            }
        return bmp;
    }//convertToBitmap
于 2013-10-11T11:18:46.327 回答