【问题标题】:Decode the file to Bitmap in Android throwing exception [duplicate]在Android抛出异常中将文件解码为位图[重复]
【发布时间】:2012-11-20 14:50:03
【问题描述】:

可能重复:
Android: Strange out of memory issue while loading an image to a Bitmap object

我开发了将文件解码为位图的应用程序。我做了以下代码,但它总是抛出内存异常

private Bitmap decodeFile(File f) {
            Bitmap bmp = null;
    FileInputStream fis = null;
    try {
        // decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inTempStorage = new byte[16*1024];
        o.inJustDecodeBounds = true;
        o.inPurgeable=true;
        //if(!f.exists()) f.createNewFile();

        if(!f.exists()) f.createNewFile();

        fis = new FileInputStream(f);
        BitmapFactory.decodeStream(fis, null, o);
        try {
            fis.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        fis = null;
        // Find the correct scale value. It should be the power of 2.
        final int REQUIRED_SIZE = 70;
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE
                    || height_tmp / 2 < REQUIRED_SIZE)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        // decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inTempStorage = new byte[16*1024];
        o2.inSampleSize = scale;            
        o2.inPurgeable=true;
        o2.inTempStorage = new byte[16*1024];

        fis = new FileInputStream(f);
        bmp = BitmapFactory.decodeStream(fis);
        if (fis != null)
            fis.close();
        fis = null;
        return bmp;
        // return BitmapFactory.decodeFile(f.getAbsolutePath(), o2);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {

        System.gc();
        try {
            if (fis != null)
                fis.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
    }
        fis = null;

    }
          return null;
}

添加了日志猫

12-03 12:03:05.070: E/AndroidRuntime(30696): FATAL EXCEPTION: main
12-03 12:03:05.070: E/AndroidRuntime(30696): java.lang.OutOfMemoryError
12-03 12:03:05.070: E/AndroidRuntime(30696):    at  android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
12-03 12:03:05.070: E/AndroidRuntime(30696):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:527)
12-03 12:03:05.070: E/AndroidRuntime(30696):    at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:301)
12-03 12:03:05.070: E/AndroidRuntime(30696):    at com.dexterity.iPinion.RootActivity.readFromSDCard(RootActivity.java:1984)
12-03 12:03:05.070: E/AndroidRuntime(30696):    at com.dexterity.iPinion.RootActivity.setBrandingImage(RootActivity.java:285)
12-03 12:03:05.070: E/AndroidRuntime(30696):    at com.dexterity.iPinion.RootActivity.access$0(RootActivity.java:252)
12-03 12:03:05.070: E/AndroidRuntime(30696):    at com.dexterity.iPinion.RootActivity$1.handleMessage(RootActivity.java:515)
12-03 12:03:05.070: E/AndroidRuntime(30696):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-03 12:03:05.070: E/AndroidRuntime(30696):    at android.os.Looper.loop(Looper.java:137)
12-03 12:03:05.070: E/AndroidRuntime(30696):    at  android.app.ActivityThread.main(ActivityThread.java:4745)
12-03 12:03:05.070: E/AndroidRuntime(30696):    at java.lang.reflect.Method.invokeNative(Native Method)
12-03 12:03:05.070: E/AndroidRuntime(30696):    at java.lang.reflect.Method.invoke(Method.java:511)
12-03 12:03:05.070: E/AndroidRuntime(30696):    at  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-03 12:03:05.070: E/AndroidRuntime(30696):    at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-03 12:03:05.070: E/AndroidRuntime(30696):    at dalvik.system.NativeStart.main(Native   Method)

【问题讨论】:

  • user4232 编辑了问题

标签: android image download


【解决方案1】:

您正在设置 BitmapFactory.Options,但您没有将它们传递给您的 BitmapFactory.decodeStream。

你有:

BitmapFactory.decodeStream(fis); 

应该是:

BitmapFactory.decodeStream(fis, o2); 

另请阅读Android's displaying bitmaps

此外,根据您的设备,您可能只有很少的运行时内存。对于 24MB 或更少的旧设备来说尤其如此,大多数较新的设备是 64MB 或更大。你可以检查

Runtime.getRuntime().maxMemory()

此外,您可以通过将 'android:largeHeap="true" ' 添加到 AndroidManifest.xml 文件来强制使用大堆。在我的 Galaxy Nexus 上将我的堆推到 254MB。

【讨论】:

  • 我试过了也不行
  • 您是否阅读了我提供的文档链接?它涵盖了一切。
  • 谢谢@esse !!!添加 android:largeHeap="true" 防止我的​​应用程序由于内存不足异常而崩溃:)
【解决方案2】:

Android 内存非常低,大约 14mb

要在您的应用中使用位图,请使用此链接, 使用bitmpas的官方android链接

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 2013-03-07
    相关资源
    最近更新 更多