为什么android要求为小照片分配大量内存?

时间:2017-08-29 14:07:23

标签: android crash out-of-memory

我正在尝试在我的Android手机上加载大小为965KB的jpeg照片。代码如下。当我运行代码时,应用程序崩溃了。

Button _btn = (Button) findViewById(R.id.btn1);
_btn.setOnClickListener(new View.OnClickListener() {

     @Override
      public void onClick(View view) {
           Drawable _draw  = getResources().getDrawable(R.drawable.sea, null);
    }
});

这是崩溃日志。

  

E / AndroidRuntime:致命异常:主要                                                                            处理:com.a3sumatch.multipart,PID:29240                                                                            java.lang.OutOfMemoryError:无法分配362797068字节   分配8388608个空闲字节和254MB直到OOM                                                                                at dalvik.system.VMRuntime.newNonMovableArray(Native Method)                                                                                在android.graphics.BitmapFactory.nativeDecodeAsset(本机方法)                                                                                在android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)                                                                                在   android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)                                                                                在   android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)                                                                                在   android.content.res.Resources.loadDrawableForCookie(Resources.java:2761)                                                                                在android.content.res.Resources.loadDrawable(Resources.java:2654)                                                                                在android.content.res.Resources.getDrawable(Resources.java:833)                                                                                在   com.a3sumatch.multipart.MainActivity $ 1.onClick(MainActivity.java:40)                                                                                在android.view.View.performClick(View.java:5205)                                                                                在android.view.View $ PerformClick.run(View.java:21176)                                                                                在android.os.Handler.handleCallback(Handler.java:739)                                                                                在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                在android.os.Looper.loop(Looper.java:171)                                                                                在android.app.ActivityThread.main(ActivityThread.java:5611)                                                                                at java.lang.reflect.Method.invoke(Native Method)                                                                                在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:732)                                                                                在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)

1 个答案:

答案 0 :(得分:2)

  

我正在尝试在我的Android手机上加载大小为965KB的jpeg照片

这是一张绝对庞大的照片。 JPEG,如PNG和WebP,是一种压缩文件格式。从透视角度来看,我的秃头的3229x2480照片是JPEG的829.3KB。我希望你的每个维度都要大一些。该分辨率高于我所知道的任何Android设备屏幕的分辨率。

您的OutOfMemoryError用于362797068字节的分配。 Bitmap是图像的未压缩版本。 362797068字节相当于大约9524x9524方形图像(和4字节/像素)。

所以,你需要做两件事:

  1. 如果您在res/drawable/中有此图片,请将其移至res/drawable-nodpi/

  2. 沿每个维度将此图片的分辨率降低至少四分之一