decodeStream returns null

三世轮回 提交于 2019-11-29 08:23:48

The problem was that once you've used an InputStream from a HttpUrlConnection, 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. Otherwise we have to abort the http request.

request.abort();

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

There are some problems in the replies of the second floor. Because in the Method of copy(), inputstream has been used,so in the method of decodeSampledBitmapFromInputStream(in,copyOfIn),we can not catch the value of options.outWidth.

Here I did some correction;We can convert each other between byte[] and inputstream So,we can convert inputstream to byte[], This can be used multiple times.

Code as follows:

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;  
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!