Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
787 views
in Technique[技术] by (71.8m points)

android - decodeStream returns null

I'm trying to adopt bitmap resizing tutorial - the only difference is that I use decodeStream instead of decodeResource. It's strange but decodeStream, without any manipulations, gives me a bitmap obj, but when I go through decodeSampledBitmapFromStream it returns null for some reason. How do I fix it ?

Here's the code I use:

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;
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Because the InputStream Object can only be consumed once, you have to make a deep copy of an InputStream Object when you want to resize the bitmap from inputStream of HttpUrlConnection,otherwise decodeStream will returns null.Here is one possible solution:

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

the souce code of Method copy is as follows:

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

the souce code of Method decodeSampledBitmapFromInputStream is as follows:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...