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