![](/img/trans.png)
[英]App crashes with java.lang.OutOfMemoryError in android sometimes
[英]Android: App crashes on the device while uploading images,java.lang.outofMemoryError
在我的Android應用程序中,我必須允許用戶單擊按鈕以打開圖庫並選擇圖像。 然后需要將特定的選定圖像加載到我的布局(UI)中的圖像視圖中。 我有一些代碼,但它來自java.lang.outofmemory.Please任何人都可以幫助我?
您應該在onActivityResult()方法上解碼圖像uri。 將此方法調用decodeBitmap。
/**
* This is very useful to overcome Memory waring issue while selecting image
* from Gallery
*
* @param selectedImage
* @param context
* @return Bitmap
* @throws FileNotFoundException
*/
public static Bitmap decodeBitmap(Uri selectedImage, Context context)
throws FileNotFoundException {
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(context.getContentResolver()
.openInputStream(selectedImage), null, o);
final int REQUIRED_SIZE = 100;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
break;
}
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(context.getContentResolver()
.openInputStream(selectedImage), null, o2);
}
有關詳細信息,請參閱有效顯示位圖主題
http://developer.android.com/training/displaying-bitmaps/index.html
希望這個幫助。
在這里,您將找到為什么會出現此異常以及如何正確顯示位圖: http : //developer.android.com/training/displaying-bitmaps/index.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.