SDカードまたはリソースから大きな画像ファイルを選択する際のメモリ不足エラーを回避します。どうすればこの問題に取り組むことができますか?
3 に答える
ビットマップでSDカードからファイルを選択するとき、またはsetImageURIを使用してSDカードにファイルを選択する際のエラーを回避するには、次の方法を使用します。
public static Bitmap decodeScaledBitmapFromSdCard(String filePath,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);
}
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) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
inSampleSizeを使用して、占有するメモリを減らすことができます。
これがコードです
public static Bitmap decodeAndResizeFile(File f) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 70;
// Find the correct scale value. It should be the power of 2.
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;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
}
return null;
}
ここでは、を使用して画像をデコードしinSampleSize
、このコードはあなたに最適なinSampleSize
値を見つけます。
それは私にとってはうまくいきました。
上記のコードを使用したくない場合は、未使用のメモリを使用bitmap.recycle()
しSystem.gc()
て解放することもできますが、上記のコードは問題なく機能します。2つのうちどちらでも使用できます。
objbitmap.recycle();
objbitmap = null;
System.gc();
うまくいけば、これで問題が解決するかもしれません!
これは、最終的にどのサイズにするかがわかっているかどうかによって異なります。
サイズがわかっている場合:
Android_Crakerによる回答を参照してください。それは問題への確かなアプローチです。
サイズがわからない場合:
この場合、画像をメモリに収まる大きさにする必要があります。解決策は、適切なサイズが見つかるまでダウンサンプルをループすることです。BitmapFactory.Options options = new BitmapFactory.Options();
boolean done = false;
int downsampleBy = 1;
Bitmap bitmap = null;
while (!done) {
options.inSampleSize = downsampleBy++;
try {
bitmap = BitmapFactory.decodeFile(filePath, options);
done = true;
catch (OutOfMemoryError e) {
// Ignore. Try again.
}
}