2017-09-06 104 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

相關問題