BitmapFactory.de codeStream返回null,则选项被设置 [英] BitmapFactory.decodeStream returning null when options are set

查看:117
本文介绍了BitmapFactory.de codeStream返回null,则选项被设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 BitmapFactory.de codeStream(InputStream的)问题。使用时不带选项,它会返回一个图像。但是,当我使用它的选项, .DE codeStream(的InputStream,空,选项)它永远不会返回位图。

我想要做的就是下采样位图之前,我其实加载它以节省内存。 我读过一些好的导游,但没有使用 .DE codeStream

  • <一个href="http://groups.google.com/group/android-developers/browse_thread/thread/1e9d19c3adf1a768/afef1c8341a63ad2?lnk=gst&q=samuh#afef1c8341a63ad2">Handling大位图

  • 这里

  • 在Android的 <
  • 图像处理/ EM>

工作得很好

 网​​址URL =新的URL(SURL);
HttpURLConnection的连接=(HttpURLConnection类)url.openConnection();

InputStream的是= connection.getInputStream();
位图IMG = BitmapFactory.de codeStream(是,空,期权);
 

不工作

 的InputStream是= connection.getInputStream();
位图IMG = BitmapFactory.de codeStream(是,空,期权);

InputStream的是= connection.getInputStream();

选项​​选项=新BitmapFactory.Options();
options.inJustDe codeBounds = TRUE;

BitmapFactory.de codeStream(是,空,期权);

布尔scaleByHeight = Math.abs(options.outHeight  -  TARGET_HEIGHT)&GT; = Math.abs(options.outWidth  -  TARGET_WIDTH);

如果(options.outHeight * options.outWidth * 2  - ; = 200 * 100 * 2){
    //加载,扩展到2个最小的功率会得到它&LT; =所需的尺寸
    双采样大小= scaleByHeight
    ? options.outHeight / TARGET_HEIGHT
    :options.outWidth / TARGET_WIDTH;
    options.inSampleSize =
        (INT)Math.pow(2D,Math.floor(
        将Math.log(采样大小)/Math.log(2D)));
}

//做实际的解码
options.inJustDe codeBounds = FALSE;
位图IMG = BitmapFactory.de codeStream(是,空,期权);
 

解决方案

但问题是,一旦你使用一个InputStream从HttpURLConnection类来获取图像元数据,你不能倒退,并再次使用相同的InputStream。

因此​​,你必须创建一个新的InputStream为图像的实际采样。

 选项选项=新BitmapFactory.Options();
  options.inJustDe codeBounds = TRUE;

  BitmapFactory.de codeStream(是,空,期权);

  布尔scaleByHeight = Math.abs(options.outHeight  -  TARGET_HEIGHT)&GT; = Math.abs(options.outWidth  -  TARGET_WIDTH);

  如果(options.outHeight * options.outWidth * 2  - ; = 200 * 200 * 2){
         //加载,扩展到2个最小的功率会得到它&LT; =所需的尺寸
        双采样大小= scaleByHeight
              ? options.outHeight / TARGET_HEIGHT
              :options.outWidth / TARGET_WIDTH;
        options.inSampleSize =
              (INT)Math.pow(2D,Math.floor(
              将Math.log(采样大小)/Math.log(2D)));
     }

        //做实际的解码
        options.inJustDe codeBounds = FALSE;

        is.close();
        是= getHTTPConnectionInputStream(SURL);
        位图IMG = BitmapFactory.de codeStream(是,空,期权);
        is.close();
 

I'm having issues with BitmapFactory.decodeStream(inputStream). When using it without options, it will return an image. But when I use it with options as in .decodeStream(inputStream, null, options) it never returns Bitmaps.

What I'm trying to do is to downsample a Bitmap before I actually load it to save memory. I've read some good guides, but none using .decodeStream.

WORKS JUST FINE

URL url = new URL(sUrl);
HttpURLConnection connection  = (HttpURLConnection) url.openConnection();

InputStream is = connection.getInputStream();
Bitmap img = BitmapFactory.decodeStream(is, null, options);

DOESN'T WORK

InputStream is = connection.getInputStream();
Bitmap img = BitmapFactory.decodeStream(is, null, options);

InputStream is = connection.getInputStream();

Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;

BitmapFactory.decodeStream(is, null, options);

Boolean scaleByHeight = Math.abs(options.outHeight - TARGET_HEIGHT) >= Math.abs(options.outWidth - TARGET_WIDTH);

if (options.outHeight * options.outWidth * 2 >= 200*100*2){
    // Load, scaling to smallest power of 2 that'll get it <= desired dimensions
    double sampleSize = scaleByHeight
    ? options.outHeight / TARGET_HEIGHT
    : options.outWidth / TARGET_WIDTH;
    options.inSampleSize =
        (int)Math.pow(2d, Math.floor(
        Math.log(sampleSize)/Math.log(2d)));
}

// Do the actual decoding
options.inJustDecodeBounds = false;
Bitmap img = BitmapFactory.decodeStream(is, null, options);

解决方案

The problem was that once you've used an InputStream from a HttpUrlConnection to fetch image metadata, you can't rewind and use the same InputStream again.

Therefore you have to create a new InputStream for the actual sampling of the image.

  Options options = new BitmapFactory.Options();
  options.inJustDecodeBounds = true;

  BitmapFactory.decodeStream(is, null, options);

  Boolean scaleByHeight = Math.abs(options.outHeight - TARGET_HEIGHT) >= Math.abs(options.outWidth - TARGET_WIDTH);

  if(options.outHeight * options.outWidth * 2 >= 200*200*2){
         // Load, scaling to smallest power of 2 that'll get it <= desired dimensions
        double sampleSize = scaleByHeight
              ? options.outHeight / TARGET_HEIGHT
              : options.outWidth / TARGET_WIDTH;
        options.inSampleSize = 
              (int)Math.pow(2d, Math.floor(
              Math.log(sampleSize)/Math.log(2d)));
     }

        // Do the actual decoding
        options.inJustDecodeBounds = false;

        is.close();
        is = getHTTPConnectionInputStream(sUrl);
        Bitmap img = BitmapFactory.decodeStream(is, null, options);
        is.close();

这篇关于BitmapFactory.de codeStream返回null,则选项被设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆