[Android Pro] Workaround for Bitmapfactory.decodestream (IS) method not properly decoded to bitmap object

Source: Internet
Author: User

In the Android SDK 1.6 release API help documentation, the Help documentation for BitmapFactory.decodeFactory.decodeStream (InputStream is) explains this:

< Strong > If the input stream is null, or cannot be used to decode a bitmap, the function returns null</Strong C6>>. The stream's position'll be where ever it is after the encoded data is read. Parametersis  
 Public StaticBitmap 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 (); }    returnresult;}

Later debug found that result is null, plus the view in the Help document in bold words,
So in the case of the obtained InputStream is not empty, call the Bitmapfactory.decodestream (is) method, he also may not be able to decode into bitmap, just at the beginning I suspect that it is itself a picture address problem, or the picture itself is not formatted correctly, But through the browser view, the picture shows normal, and, I saved dozens of pictures, but each time there will be a few pictures can not be normal display, need to download three or four times, it is possible to save the success.

Later in an article found that this is the Android 1.6 version of a bug!

There was a solution that the bull had proposed, and I tried, and the problem solved

First, in the original method to change a sentence:

result = Bitmapfactory.decodestream (new patchinputstream (input));

Create a second class:

 Public classPatchinputstreamextendsfilterinputstream{protectedPatchinputstream (InputStream in) {Super(in); //TODO auto-generated Constructor stub        }                 Public LongSkipLongNthrowsioexception{Longm=0l;  while(m<N) {                Long_m=in.skip (nm); if(_m==0l){                     Break; } m+=_m; }            returnm; }    }

The second method: The final use of this method

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 picture byte stream array size **/     Public Static byte[] Readstream (InputStream instream)throwsexception{Bytearrayoutputstream OutStream=NewBytearrayoutputstream (); byte[] buffer =New byte[1024]; intLen = 0;  while((len=instream.read (buffer))! =-1) {outstream.write (buffer,0, Len);              } outstream.close ();              Instream.close (); returnOutstream.tobytearray (); }

[Android Pro] Workaround for Bitmapfactory.decodestream (IS) method not properly decoded to bitmap object

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.