如何使用decodeSampledBitmapFromResource下载图片?

时间:2013-09-28 18:57:08

标签: android bitmap out-of-memory

我从互联网上下载照片,然后将其保存在Bitmap变量中。

我试图修复它导致的崩溃(它是一个内存问题)。

这是他们在这里建议的代码:Loading Bitmaps

但是他们只讨论来自资源的图像,所以我卡住了..

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
        int reqWidth, int 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);
}

我可以通过某种方式将其转换为使用下载的位图吗?

3 个答案:

答案 0 :(得分:3)

您可以使用BitmapFactory.decodeStream(inputStream, null, options);从inputStream进行解码。但是,它只能运行一次,因为inputStream只能使用一次。因此,您无法通过两次调用inSampleSize来真正计算decodeStream。如果您知道要下载的图像的大小,请尝试对inSampleSize进行硬编码。

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 2; \\ hard code it to whatever is reasonable
    return BitmapFactory.decodeStream(inputStream, null, options);

答案 1 :(得分:1)

是的,你可以解码Bitmap。我建议你动态计算inSampleSize。

public static Bitmap decodeSampledBitmapFromResource(Context context, Uri uri,
                                                     int reqWidth, int reqHeight) 
                                                   throws FileNotFoundException {
    ContentResolver contentResolver = context.getContentResolver();
    InputStream inputStream = contentResolver.openInputStream(uri);
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(inputStream, null, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    inputStream = contentResolver.openInputStream(uri);
    return BitmapFactory.decodeStream(inputStream, null, options);
}

答案 2 :(得分:1)

有一种方法可以从InputStream计算inSampleSize: 关键是我们应该缓存从inputStream

读取的数据
InputStream in = conn.getInputStream();
byte[] data = Utils.streamToBytes(in);
BitmapFactory.Options option = new BitmapFactory.Options();
option.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(data, 0, data.length, option);
option.inSampleSize = Utils.getBitmapSampleSize(option.outWidth, reqWidth);
option.inJustDecodeBounds = false;
return BitmapFactory.decodeByteArray(data, 0, data.length, option);

Utils.streamToBytes:

 byte[] buffer = new byte[1024];
 ByteArrayOutputStream output = new ByteArrayOutputStream();
 int len = 0;
 while((len = in.read(buffer)) != -1) {
        output.write(buffer, 0, len);
 }
 output.close();
 in.close();
 return output.toByteArray();