2017-09-06 102 views
1

我写了一个应用程序,到目前为止,我总是在sdk level 25上构建它。现在我想尝试在较低级别上运行它,但它崩溃。虽然调试我想通了,这个链接给出了错误:在SDK 22上安装应用程序在Bitmapfactory导致错误

bitmapBackgroundColors = BitmapFactory.decodeResource(this.getResources(), R.drawable.background_colors); 

你有任何想法如何摧毁像一切? :D Bitmapfactory自SDK 1以来一直存在,所以这真的不能成为问题。

我compileSdkVersion是25,我的minSdkVersion 15和我targetSdkVersion 25(虽然我仍然不明白RLY什么这些数字的含义)

编辑:该错误是一个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) 

编辑2:所以可能我犯了一些图像解码的错误。 我怎么能做到这一点的效率比是:

bitmapBackgroundColors = BitmapFactory.decodeResource(this.getResources(), R.drawable.background_colors); 
bitmapBackgroundColors = Bitmap.createScaledBitmap(bitmapBackgroundColors, screenX * 3, screenY, false); 
+0

什么是logcat中显示错误消息(由???)。 – Jorgesys

+0

看看编辑 – MilamberMilamber

回答

1

使用BitmapFactory.decodeResource() doesn't没有什么都没有做的SDK级别:

bitmapBackgroundColors = BitmapFactory.decodeResource(this.getResources(), R.drawable.background_colors); 

这里的问题

java.lang.OutOfMemoryError: Failed to allocate a 223948812 byte allocation with 8134272 free bytes and 180MB until 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); 

这是另一篇文章来优化您的图片在你的项目中使用:

Reducing Image Download Sizes

+0

好吧,谢谢。 “500”的意思是什么? – MilamberMilamber

+0

我的意思是它应该是宽度和高度的图片我想画,但不是,因为即使当我把两次相同的数字图片不会变形 – MilamberMilamber

+0

不,我的意思是作为一个例子,你会得到一个更小原始图像的版本为位图,在这个500 x 500px的例子中,这意味着最小的字节数量。图像background_colors的大小是多少字节? – Jorgesys

相关问题