java - Android BinaryFactory.decodeStream always return null -


I am trying to download the image and decode bitmap for bitmap, but decode stream always gives space is. I have raised many similar questions, tried many examples, but the solution was not found.

Here is my code:

  the public class extends the main activity activity {@Override protected Crete on the zero (Bundle saved instance) {super. Instant state); SetContentView (R.layout.activity_main); } Public Zero Download Button (see V) {String image url = "http://www.picgifs.com/bird-graphics/bird-graphics/elf-owl/bird-graphics-elf-owl-527150.bmp"; New thread (New ImageDownloader (imageUrl)) Start (); } Public Zero Show Image Button (see V) {bitmap image = ImageHandler.getImage (); If log (image == zero) E ("error", "no image is available"); Else {ImageView imageView = (ImageView) findViewById (R.id.imageView); ImageView.setImageBitmap (image); }}} Class ImageHandler {static protected list & lt; Bitmap & gt; ImagesList; Fixed public Zero addImage (bitmap image) {imagesList.add (image); } Fixed public bitmap getImage () {if (ImagesList.isEmpty ()) images returnlistList.get (0); And tap return; }} Class ImageDownloader implements Runnable {public bitmap bmpImage; Private string urlString; Public image downloader (string url string) {this.urlString = urlString; } Public Zero Run () {Android HTT Client Client = AndroidHat Client.New instance ("Android"); HttpGet getRequest = new HttpGet (urlString); HttpResponse response = client.execute (getRequest); HttpEntity unit = response.getEntity (); InputStream inputstream = faucet; Inputstream = (new buffertet entity) GetContent (); BmpImage = Bitmapfichture Decodestream (inputstream); // bmpImage = BitmapFactory.decodeStream (new URL (urlString) .openConnection (). GetInputStream ()); } Hold (exception e) {Log.e ("ImageDownloadError", e.toString ()); Return; } If (bmpImage! = Null) {ImageHandler.addImage (bmpImage); Logs. I ("Info", "Download Image Completely"); } And logs E ("error", "bitmap is empty"); }}   

ps showImageButton throws IllegalStateException , but I was sick already.

I think the problem is that once you use an InputStream from an HttpUrlConnection , You can not reverse it again and use the same InputStream again. So you have to create a new InputStream for the actual sampling of the image. Otherwise, we have to cancel the HP request.

If you are trying to download another image, then only once we can use the inputstream for httprequest, then this will be an error in the form of a "already created inputstream" Will throw. So we need httprequest.abort ();

httprequest must be canceled after downloading;

  HttpGet httpRequest = new HttpGet (URI.create (path)); Http client http client = new defaultHttpClient (); HttpResponse response = (HTTPPS) httpclient.execute (httpRequest); HttpEntity unit = response.getEntity (); Buffer httptextf buff html entrant = new buffed htpnt (unit); BMP = Bitmapfinder Encoded stream (buff.tttt.atcontent ()); HttpRequest.abort ();    

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -