Android 在SDK 22上安装应用程序导致Bitmapfactory出错

Android 在SDK 22上安装应用程序导致Bitmapfactory出错,android,bitmap,sdk,version,Android,Bitmap,Sdk,Version,我编写了一个应用程序,到目前为止,我总是在sdk级别25上构建它。现在我想尝试在较低的级别上运行它,但它崩溃了。调试时,我发现此链接会出现错误: bitmapBackgroundColors = BitmapFactory.decodeResource(this.getResources(), R.drawable.background_colors); 你知道它怎么会像所有东西一样毁灭吗D Bitmapfactory自SDK 1以来一直处于活动状态,因此这不是问题所在 我的编译器DKVers

我编写了一个应用程序,到目前为止,我总是在sdk级别25上构建它。现在我想尝试在较低的级别上运行它,但它崩溃了。调试时,我发现此链接会出现错误:

bitmapBackgroundColors = BitmapFactory.decodeResource(this.getResources(), R.drawable.background_colors);
你知道它怎么会像所有东西一样毁灭吗D Bitmapfactory自SDK 1以来一直处于活动状态,因此这不是问题所在

我的编译器DKVersion是25,我的MinsdkVersion15和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)
使用“不”与sdk级别无关:

bitmapBackgroundColors = BitmapFactory.decodeResource(this.getResources(), R.drawable.background_colors);
这里的问题

java.lang.OutOfMemoryError:未能分配223948812字节 分配8134272个可用字节和180MB到OOM

每个设备上的内存存储容量是不同的。所以你必须优化你的图像

查看本文并提供优化图像的提示:

作为示例,您将使用此方法

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);
这是另一篇优化项目中使用的图像的文章:


LogCat中显示的错误消息是什么(由???引起)。请查看编辑我的意思是它应该是我想要绘制的图片的宽度和高度,但不是,因为即使我把两个相同的数字放在一起,图片也不会被转换。不,我的意思是,作为一个例子,你会得到一个更小版本的原始图像,作为位图,在这个例子中,500 x 500px意味着最少的字节数。图像背景颜色的大小(以字节为单位)是多少?它是一幅3240x120图片,有125.883字节3240x120只有125883字节或KB?确切地说是字节。这就是我如此困惑的原因。当我画这幅画时,它需要的空间是它实际“重量”的10倍。
//bitmapBackgroundColors = BitmapFactory.decodeResource(this.getResources(), R.drawable.background_colors);
bitmapBackgroundColors = 
decodeSampledBitmapFromResource(this.getResources(), R.drawable.background_colors, 500, 500);