在將背景圖像設置爲Android視圖(API 22)的過程中,我遇到了一些問題。高效設置Android視圖背景圖像給出java.lang.OutOfMemoryError
我的照片是1MB .jpeg文件,4000x2672。
起初我經由
android:background="@drawable/your_image"
方法嘗試,得到一個d/Skia的:---分配失敗了縮放的位圖錯誤。
然後我也跟着http://developer.android.com/training/displaying-bitmaps/load-bitmap.html並執行如下它:
BitmapDecoder.java
(..omissis..)
public class BitmapDecoder {
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height/2;
final int halfWidth = width/2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight/inSampleSize) > reqHeight
&& (halfWidth/inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
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);
}
}
SplasActivity.java(主要活動)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
LinearLayout mL = (LinearLayout)findViewById(R.id.MainLayout);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
if (Build.VERSION.SDK_INT >= 16) {
Drawable d = new BitmapDrawable(getResources(),
BitmapDecoder.decodeSampledBitmapFromResource(getResources(),
R.drawable.andromeda,
1080,1920));
mL.setBackground(d);
}
this.getSupportActionBar().hide();
}
我獲得設備屏幕的WxH,調整Drawable資源的大小e轉換爲該尺寸,將生成的位圖轉換爲Drawable,將Drawable設置爲View背景。
但我總是得到
D/skia﹕ --- allocation failed for scaled bitmap
...
java.lang.OutOfMemoryError: Failed to allocate a 684032012 byte allocation with 16777120 free bytes and 227MB until OOM
錯誤。
我一直在尋找幾小時,並實施所有建議的技術,沒有運氣。作爲一個側面說明,如果我把寬度和高度設置爲一個非常小的值(例如400x200),它就可以工作。但是,正如你所想象的那樣,這是不可行的。
關於如何解決任何想法?
?從logcat發佈stacktrace,'calculateInSampleSize'返回什麼? – pskink
stacktrace from logcat:http://pastebin.com/2nxTDjLb –
calculateInSampleSize返回1,屏幕分辨率爲1440 x 2560(Samsung Galaxy S6) –