我想将一个15Mb的图像文件加载到一个imageview中..我尝试使用piccasa和其他一些方法加载它,但都返回内存异常。
我使用的代码是
Picasso.with(getActivity()).load(R.drawable.highqual).into(imageView);
有没有办法让它在Android
中成为可能答案 0 :(得分:2)
试试这个:
调用此函数decodeSampledBitmapFromFile(String filePath,int reqWidth, int reqHeight)
reqHeight
和reqWidth
是您的ImageView
维度。另请注意,如果需要很长时间并发送给您ANR
,请将其设置为单独的thread
,然后将其设置为ImageView
。
代码来自doc,我将其更改为读取图像文件。
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;
}
public static Bitmap decodeSampledBitmapFromFile(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);
}
参考: