0
在嘗試從BitmapFactory中找到解決內存不足錯誤的解決方案後,發現本教程。 http://developer.android.com/training/displaying-bitmaps/load-bitmap.html從圖庫中獲取資源ID
我明白所有的它除了線 mImageView.setImageBitmap( decodeSampledBitmapFromResource(getResources(),R.id.myimage,100,100));
第二個參數要求圖像的資源ID,我不確定它來自哪裏。這是我的代碼如下所示:
Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadImage);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
//http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.id.imgView, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;
ImageView imageView = (ImageView) findViewById(R.id.imgView);
//imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
imageView.setImageBitmap(
decodeSampledBitmapFromResource(getResources(),res, imageView.getWidth(), imageView.getHeight()));
cursor.close();
}
}
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;
} 公共靜態位圖decodeSampledBitmapFromResource(資源RES,INT渣油, INT reqWidth,詮釋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);
}
我原本使用的是\t \t \t imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 它在較小的圖像上工作得很好。對於任何大型圖像,我都會遇到內存不足的情況。有沒有辦法將picturePath轉換爲字符串? – Naughtlok
你爲什麼要改變一個刺痛的路徑?無論如何你必須閱讀圖片文件。嘗試使用'BitmapFactory.decodeFile(picturePath,options)',對於這些選項,在您的問題的'decodeSampledBitmapFromResource()'方法中使用帶有'options.inSampleSize'的位。這樣,你會得到一個不應該打動你的記憶的小圖片。 – Ridcully