【问题标题】:How can I use decodeSampledBitmapFromResource With a downloaded pic?如何将 decodeSampledBitmapFromResource 与下载的图片一起使用?
【发布时间】:2013-09-28 18:57:08
【问题描述】:

我从 Internet 下载照片,然后将其保存在 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);
}

我能否以某种方式对其进行转换以使其与下载的位图一起使用?

【问题讨论】:

    标签: android bitmap out-of-memory


    【解决方案1】:

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

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

    【讨论】:

    • 好吧,如果解码流是正确的,你应该能够从 headers/meta 中获取图像尺寸,而无需下载整个图像。
    【解决方案2】:

    是的,您可以解码位图。我建议你动态计算 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);
    }
    

    【讨论】:

      【解决方案3】:

      有一种方法可以从 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();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-05
        • 2019-04-02
        • 1970-01-01
        相关资源
        最近更新 更多