重複の可能性:
Android: 画像をビットマップ オブジェクトにロードする際のメモリ不足の問題
OutOfMemoryError: ビットマップ サイズが VM の予算を超えています:- Android
ユーザーが写真を撮ったり、ギャラリーに既に存在するものを使用したり、データベースにニュースを保存した後、ニュースに添付したりできるアプリケーションがあります(画像パスのコンテンツのみを保存しています:/ / ...)、表示します画像のタイトルと日付を含む ListView 内のすべてのニュース。アダプターに画像を表示するには、次を使用しています。
...
image.setImageURI(null);
System.gc();
image.setImageURI(Uri.parse(noticia.getIMAGEM()));
...
ただし、 System.gc (); を使用しても。読み込まれる新しい画像ごとに
12-12 14:59:37.239: E/AndroidRuntime (4997): java.lang.OutOfMemoryError: ビットマップ サイズがパフォーマー VM の予算を超えています
また、すでに使用してみました
image.setImageURI(null);
System.gc();
onResume()、onPause()、onDestroy() で、しかし何も機能しませんでした。
この投稿もお読みください: [リンク][1]
if(!((BitmapDrawable)image.getDrawable()).getBitmap().isRecycled()){
((BitmapDrawable)image.getDrawable()).getBitmap().recycle();
}
Bitmap thumbnail = null;
try {
thumbnail = MediaStore.Images.Media.getBitmap(context.getContentResolver(), Uri.parse(img));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
image.setImageBitmap(thumbnail);
しかし、次のエラーが表示されます。
12-12 15:28:42.879: E/AndroidRuntime(5296): java.lang.RuntimeException: Canvas: リサイクルされたビットマップ android.graphics.Bitmap@40756520 を使用しようとしています
それに加えて、リストビューを上下にスクロールするとクラッシュし続けるので、画像を要求していると思います..
他に何をすればいいのかわからないのですが、ヒントはありますか?
編集:
この問題に対する同じ解決策の別の投稿が完全に機能することがわかりましたが、リストを上下にスクロールするときに彼女が戦ったものであり、これはユーザーエクスペリエンスにとって非常に悪いため、画像のキャッシュが必要になると思います。解決するアイデアはありますか?
次のコード:
アダプター内: ...
String imagePath = getPath(Uri.parse(img));
image.setImageBitmap(decodeSampledBitmapFromResource(imagePath, 85, 85));
...
メソッド:
public String getPath(Uri uri)
{
Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
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 = 2;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromResource(String resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(resId, options);
}