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