3

我正在尝试采用位图调整大小教程 - 唯一的区别是我使用 decodeStream 而不是 decodeResource。这很奇怪,但没有任何操作的 decodeStream 给了我一个位图 obj,但是当我通过 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;
    }
4

3 回答 3

7

由于 InputStream 对象只能被消费一次,当你想从 HttpUrlConnection 的 inputStream 中调整位图大小时,你必须对 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);
             }
         }
     }

Method 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;
 }

Method 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);
}
于 2012-10-09T10:01:04.517 回答
4

问题是,一旦您使用了来自 HttpUrlConnection 的 InputStream,您就不能倒带并再次使用相同的 InputStream。因此,您必须为图像的实际采样创建一个新的 InputStream。否则我们必须中止 http 请求。

request.abort();
于 2012-05-24T04:12:48.650 回答
2
于 2013-01-12T04:52:52.123 回答