我写了一个应用程序,到目前为止我总是在sdk级别25上构建它。现在我想尝试在较低级别运行它但它崩溃了。在调试时我发现这个链接给出了错误:
bitmapBackgroundColors = BitmapFactory.decodeResource(this.getResources(), R.drawable.background_colors);
你知道它如何像所有东西一样毁灭吗? :d 从SDK 1开始,Bitmapfactory仍然存在,所以这不能解决问题。
我的compileSdkVersion是25,我的minSdkVersion 15和我的targetSdkVersion 25.(虽然我仍然不明白这些数字是什么意思)
编辑:错误是OutOfMemory错误。但它必须加载的.jpg只有128kb大
java.lang.OutOfMemoryError: Failed to allocate a 223948812 byte allocation with 8134272 free bytes and 180MB 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.BitmapFactory.decodeResource(BitmapFactory.java:467)
at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:497)
at com.cucumbertroup.strawberry.strawberry.GameView.<init>(GameView.java:136)
at com.cucumbertroup.strawberry.strawberry.MainActivity.onCreate(MainActivity.java:29)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
at android.app.ActivityThread.access$800(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5389)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
Edit2:所以我可能在解码图像时犯了一些错误。 我怎么能比这更有效率呢:
bitmapBackgroundColors = BitmapFactory.decodeResource(this.getResources(), R.drawable.background_colors);
bitmapBackgroundColors = Bitmap.createScaledBitmap(bitmapBackgroundColors, screenX * 3, screenY, false);
答案 0 :(得分:1)
使用BitmapFactory.decodeResource()并不与sdk级别无关:
bitmapBackgroundColors = BitmapFactory.decodeResource(this.getResources(), R.drawable.background_colors);
这里的问题
java.lang.OutOfMemoryError:无法分配223948812字节 分配8134272个空闲字节和180MB直到OOM
是每个设备上的内存容量不同。所以你必须优化你的图像。
查看这篇文章,了解优化图片的技巧:
Loading Large Bitmaps Efficiently
Handling Bitmaps decodeResource
作为示例,您将使用此方法
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
并调用它来获得原始图像的较小版本:
//bitmapBackgroundColors = BitmapFactory.decodeResource(this.getResources(), R.drawable.background_colors);
bitmapBackgroundColors =
decodeSampledBitmapFromResource(this.getResources(), R.drawable.background_colors, 500, 500);
这是另一篇优化您的项目中使用的图像的文章: