解码流返回null。

3
我正在尝试采用位图调整大小教程-唯一的区别是我使用decodeStream而不是decodeResource。奇怪的是,decodeStream在没有任何操作的情况下给我一个位图对象,但是当我通过decodeSampledBitmapFromStream进行操作时,由于某种原因返回null。 我该如何修复它?
这是我使用的代码:
protected Handler _onPromoBlocksLoad = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            PromoBlocksContainer c = (PromoBlocksContainer) _promoBlocksFactory.getResponse();
            HttpRequest request = new HttpRequest(c.getPromoBlocks().get(0).getSmallThumbnail());

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            InputStream stream;
            ImageView v = (ImageView) findViewById(R.id.banner);
            try {
                stream = request.getStream();

                //v.setImageBitmap(BitmapFactory.decodeStream(stream)); Works fine
                Bitmap img = decodeSampledBitmapFromStream(stream, v.getWidth(), v.getHeight());
                v.setImageBitmap(img);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    };

    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            if (width > height) {
                inSampleSize = Math.round((float)height / (float)reqHeight);
            } else {
                inSampleSize = Math.round((float)width / (float)reqWidth);
            }
        }
        return inSampleSize;
    }

    public static Bitmap decodeSampledBitmapFromStream(InputStream res, int reqWidth, int reqHeight) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(res, null, options);
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
        options.inJustDecodeBounds = false;
        Bitmap img =  BitmapFactory.decodeStream(res, null, options); // Gives  null 
        return img;
    }
3个回答

7
由于InputStream对象只能被使用一次,因此如果您想要从HttpUrlConnection的输入流中调整位图大小,您需要进行InputStream对象的深度复制,否则decodeStream将返回null。以下是一个可能的解决方案:
       HttpURLConnection urlConnection = null;
         InputStream in = null;
         InputStream in2 = null;
         try {
             final URL imgUrl = new URL(url);
             urlConnection = (HttpURLConnection) imgUrl.openConnection();
             in = urlConnection.getInputStream();

             ByteArrayOutputStream out = new ByteArrayOutputStream();
             copy(in,out);
             in2 = new ByteArrayInputStream(out.toByteArray());

             // resize the bitmap
             bitmap = decodeSampledBitmapFromInputStream(in,in2);                

         } catch (Exception e) {
             Log.e(TAG, "Error in down and process Bitmap - " + e);
         } finally {
             if (urlConnection != null) {
                 urlConnection.disconnect();
             }
             try {
                 if (in != null) {
                     in.close();
                 }
                 if (in2 != null){
                     in2.close();
                 }
             } catch (final IOException e) {
             Log.e(TAG, "Error in when close the inputstream." + e);
             }
         }
     }

copy 方法的源代码如下:

public static int copy(InputStream input, OutputStream output) throws IOException{

     byte[] buffer = new byte[IO_BUFFER_SIZE];

     int count = 0;

     int n = 0;

     while (-1 != (n = input.read(buffer))) {

         output.write(buffer, 0, n);

         count += n;

     }

     return count;
 }

decodeSampledBitmapFromInputStream 方法的源代码如下:

public static Bitmap decodeSampledBitmapFromInputStream(InputStream in,
InputStream copyOfin, int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(in, null, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeStream(copyOfin, null, options);
}

1
这对我起作用了,当从我的应用程序的私有存储中读取流并进行解码时,我遇到了此问题。但是,我没有复制,而是打开文件两次以进行读取,并将两个流传递给decodeSampledBitmapFromInputStream。即不需要copy()。谢谢,我被卡住了太长时间! - Mr. Bungle
你好,我也遇到了同样的问题,并尝试了帖子中提供的解决方案。在测试了几个设备后,它可以在一些设备上运行,但对于一些堆大小较低的设备,它又会出现内存溢出错误。我不知道该怎么办了,请帮忙。 - Dory
1
IO_BUFFER_SIZE 是什么? - Mark Buikema
谢谢。你救了我的一天。 - AmmY

4
问题在于,一旦你使用了HttpUrlConnection的InputStream,你就不能倒回并再次使用相同的InputStream。因此,您必须为实际采样图像创建一个新的InputStream。否则,我们将不得不中止http请求。
request.abort();

2
第二楼的回复存在一些问题。因为在copy()方法中使用了inputstream,所以在decodeSampledBitmapFromInputStream(in,copyOfIn)方法中无法捕获options.outWidth的值。
这里我做了一些更正:我们可以在byte[]inputstream之间进行相互转换,因此我们可以将inputstream转换为byte[],这样可以多次使用。
代码如下:
HttpURLConnection connection = null;
InputStream inputStream = null;
InputStream copyiInputStream1 = null;
InputStream copyiInputStream2 = null;
Bitmap bitmap = null;
try {
     URL url=new URL(imageUrl);
     connection=(HttpURLConnection) url.openConnection();
     connection.setConnectTimeout(8000);//设置连接超时
     inputStream = connection.getInputStream();
     byte[] data = InputStreamTOByte(inputStream);
     try {
          copyiInputStream1 = byteTOInputStream(data);
          copyiInputStream2 = byteTOInputStream(data);
     } catch (Exception e) {
          e.printStackTrace();
     }
bitmap = decodeSampledBitmapFromInputStream(copyiInputStream1,copyiInputStream2);
 /** 
 * 将InputStream转换成byte数组 
 * @param in InputStream 
 * @return byte[] 
 * @throws IOException 
 */  
public byte[] InputStreamTOByte(InputStream in) throws IOException{  

    ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
    byte[] data = new byte[1024*16];  
    int count = -1;  
    while((count = in.read(data,0,1024*16)) != -1)  
        outStream.write(data, 0, count);  

    data = null;  
    return outStream.toByteArray();  
}  

/**
 * 将byte数组转换成InputStream
 * @param in
 * @return
 * @throws Exception
 */
public InputStream byteTOInputStream(byte[] in) throws Exception{  

    ByteArrayInputStream is = new ByteArrayInputStream(in);  
    return is;  
}

这应该是被接受的答案。获取宽度和高度的完整解决方案。 - bajicdusko

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接