ในแอปของฉัน ฉันกำลังเลือกรูปภาพจากที่เก็บข้อมูลหรือถ่ายภาพโดยใช้กล้อง และรูปภาพจะตั้งค่าเป็นมุมมองรูปภาพ เมื่อทำสิ่งนี้ ฉันได้รับข้อผิดพลาดหน่วยความจำไม่เพียงพอ เพื่อหลีกเลี่ยงปัญหานี้ ฉันใช้วิธีนี้ แต่มันไม่ทำงาน ใครสามารถช่วยแก้ไขปัญหานี้ได้ หรือใครสามารถแนะนำวิธีอื่นเพื่อจัดการกับปัญหานี้ได้
public void decodeFile(String filePath) {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 1024;
// 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 < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
bitmap = BitmapFactory.decodeFile(filePath, o2);
imgView.setImageBitmap(bitmap);
}