我試圖加載JPEG照片與我的Android手機上大小965KB的。代碼如下。當我運行代碼時,應用程序崩潰。爲什麼android要求爲小照片分配大量內存?
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: FATAL EXCEPTION: main Process: com.a3sumatch.multipart, PID: 29240 java.lang.OutOfMemoryError: Failed to allocate a 362797068 byte allocation with 8388608 free bytes and 254MB until OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method) at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609) at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444) at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080) at android.content.res.Resources.loadDrawableForCookie(Resources.java:2761) at android.content.res.Resources.loadDrawable(Resources.java:2654) at android.content.res.Resources.getDrawable(Resources.java:833) at com.a3sumatch.multipart.MainActivity$1.onClick(MainActivity.java:40) at android.view.View.performClick(View.java:5205) at android.view.View$PerformClick.run(View.java:21176) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:171) at android.app.ActivityThread.main(ActivityThread.java:5611) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
[Android:java.lang.OutOfMemoryError:無法分配23970828字節分配和2097152空閒字節S和2MB直到OOM(https://stackoverflow.com/questions/32244851/androidjava-lang-outofmemoryerror-failed-to-allocate-a-23970828-byte-allocatio) – petey
你知道先說:喜歡Android的規模圖像取決於屏幕分辨率?所以它可能比原來大得多。第二位圖是一種未壓縮格式,並擁有每像素一個int,而JPEG幾乎不會被壓縮 –