BitmapFactory.decodeStream在设置选项时返回null

Rob*_*oss 87 java android image bitmap

我有问题BitmapFactory.decodeStream(inputStream).在没有选项的情况下使用它时,它将返回一个图像.但是当我将它与选项一起使用时,.decodeStream(inputStream, null, options)它永远不会返回Bitmaps.

我要做的是在实际加载之前对Bitmap进行下采样以节省内存.我读过一些很好的指南,但都没有使用.decodeStream.

工作很精细

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

InputStream is = connection.getInputStream();
Bitmap img = BitmapFactory.decodeStream(is, null, options);
Run Code Online (Sandbox Code Playgroud)

什么都不行

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);
Run Code Online (Sandbox Code Playgroud)

Rob*_*oss 111

问题是,一旦您使用来自HttpUrlConnection的InputStream来获取图像元数据,您就无法再次回放并再次使用相同的InputStream.

因此,您必须为图像的实际采样创建新的InputStream.

  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();
Run Code Online (Sandbox Code Playgroud)

  • 这是否意味着图像必须下载两次?一旦获得大小并一次获得像素数据? (16认同)
  • 我不得不说Android的Bitmap类糟透了.它使用起来令人困惑和沮丧. (5认同)

Jet*_*ieh 29

尝试使用BufferedInputStream包装InputStream.

InputStream is = new BufferedInputStream(conn.getInputStream());
is.mark(is.available());
// Do the bound decoding
// inJustDecodeBounds =true
is.reset();  
// Do the actual decoding
Run Code Online (Sandbox Code Playgroud)

  • 它一直对你有用吗?出于某种原因,我在使用此方法的一些非常具体的情况下得到null.我在这里写了一篇关于它的帖子:http://stackoverflow.com/questions/17774442/how-to-get-bitmap-information-and-then-decode-bitmap-from-internet-inputstream (2认同)