Android -OutofMemory错误 - 位图工厂

时间:2014-11-27 05:33:23

标签: android

我的代码..

mImageView_Photos.setImageBitmap(BitmapFactory.decodeFile(picturePath));

我的logcat ..

11-27 10:51:56.325: E/AndroidRuntime(3879): FATAL EXCEPTION: main
11-27 10:51:56.325: E/AndroidRuntime(3879): Process: com.virtualmaze.golfelite, PID: 3879
11-27 10:51:56.325: E/AndroidRuntime(3879): java.lang.OutOfMemoryError
11-27 10:51:56.325: E/AndroidRuntime(3879):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:620)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:376)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:402)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at com.virtualmaze.golfelite.HoleMapActivity.createImageView(HoleMapActivity.java:3839)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at com.virtualmaze.golfelite.HoleMapActivity.onActivityResult(HoleMapActivity.java:3762)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at android.app.Activity.dispatchActivityResult(Activity.java:5423)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3347)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3394)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at android.app.ActivityThread.access$1300(ActivityThread.java:135)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at android.os.Handler.dispatchMessage(Handler.java:102)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at android.os.Looper.loop(Looper.java:136)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at android.app.ActivityThread.main(ActivityThread.java:5001)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at java.lang.reflect.Method.invokeNative(Native Method)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at java.lang.reflect.Method.invoke(Method.java:515)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at dalvik.system.NativeStart.main(Native Method)

我该如何纠正这个?

4 个答案:

答案 0 :(得分:5)

当图像大小很大时,它会显示java.lang.OutOfMemoryError

试试这个解决方案,

1)您可以使用Android-Universal-Image-Loader库有效地设置图像,这是一个非常好的解决方案

OR

2)您可以在使用之前缩小位图大小...

BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);

int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight / (float) height);
int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth / (float) width);

if(heightRatio > 1 || widthRatio > 1)
{
    if(heightRatio > widthRatio)
    {
        bmpFactoryOptions.inSampleSize = heightRatio;
    }
    else
    {
        bmpFactoryOptions.inSampleSize = widthRatio;
    }
}

bmpFactoryOptions.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);

答案 1 :(得分:0)

尝试android:largeHeap =" true"在AndroidManifest.xml中的application-tag中,或者我认为你必须使用NDK来处理大图像。

答案 2 :(得分:0)

解码图像/资源的方式效率不高。

你必须始终缩放位图(适合你的图像视图大小)然后解码并显示,如果需要在后台线程中执行此操作,如异步任务(带有加载符号以获得更好的UX)。 将20MP图像解码为50X50的缩略图是无效的。它只会浪费记忆力。

Android有关于如何正确解码位图的良好文档。 检查 - http://developer.android.com/training/displaying-bitmaps/index.html

除此之外总是在任何api中放置一个try catch块,它可以使运行时失败并正确处理它,最简单的你可以做的是向用户显示一条错误消息,而不是崩溃,它会通知用户有什么问题(更好的用户体验)

希望这有帮助

此致 羞涩

答案 3 :(得分:0)

使用BitmapFactory.Options

以较低分辨率解码文件
decodeFile(String pathName, BitmapFactory.Options opts)