The bitmapfactory. decodestream (is) method cannot be decoded as a bitmap object.

Source: Internet
Author: User
 

In the android SDK 1.6 API help documentation, the help documentation for bitmapfactory. decodefactory. decodestream (inputstream is) describes the following:

 Bitmap android.graphics.BitmapFactory.decodeStream(InputStream is)public static Bitmap decodeStream (InputStream is) Since: API Level 1 Decode an input stream into a bitmap. If the input stream is null, or cannot be used to decode a bitmap, the function returns null. The stream's position will be where ever it was after the encoded data was read.Parametersis  The input stream that holds the raw data to be decoded into a bitmap. ReturnsThe decoded bitmap, or null if the image data could not be decoded. 

Note the black characters. The following code is used:

public static Bitmap bmpFromURL(URL imageURL){    Bitmap result = null;    try {        HttpURLConnection connection = (HttpURLConnection)imageURL .openConnection();        connection.setDoInput(true);        connection.connect();        InputStream input = connection.getInputStream();        result = BitmapFactory.decodeStream(input);    } catch (IOException e) {        e.printStackTrace();    }    return result;}

Later debugging found that the result is null, And you can view the italics in the help document,
Therefore, when the obtained inputstream is not empty, bitmapfactory is called. the decodestream (is) method may not be able to be decoded into bitmap. At first, I suspected there was a problem with the image address or the image format was incorrect, but I checked it in a browser, images are displayed normally, And I have saved dozens of images, but each time several images cannot be displayed normally, you need to download them three or four times to save them successfully.

Later I found out in an article that this was a bug in Android 1.6!

A solution proposed by a cool man, I tried and solved the problem.

First, modify the following statement in the original method:

result = BitmapFactory.decodeStream(new PatchInputStream(input));

Create another class:

public class PatchInputStream extends FilterInputStream{protected PatchInputStream(InputStream in) {super(in);// TODO Auto-generated constructor stub}public long skip(long n)throws IOException{long m=0l;while(m<n){long _m=in.skip(n-m);if(_m==0l){break;}m+=_m;}return m;}}

Method 2: This method is used in the end.

InputStream is = httpConn.getInputStream();

if (is == null){throw new RuntimeException("stream is null");}else{try {byte[] data=readStream(is);if(data!=null){bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);}} catch (Exception e) {e.printStackTrace();}is.close();}

/** Get the image byte stream array size **/public static byte [] readstream (inputstream instream) throws exception {bytearrayoutputstream outstream = new bytearrayoutputstream (); byte [] buffer = new byte [1024]; int Len = 0; while (LEN = instream. read (buffer ))! =-1) {outstream. Write (buffer, 0, Len);} outstream. Close (); instream. Close (); Return outstream. tobytearray ();}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.