Android questions about displaying network pictures in the ListView

Source: Internet
Author: User

The second time you download a picture in the ListView, it will appear

Skandroidcodec::newfromstream returned null

Maybe the picture is a little bigger, it's the first time it's not finished downloading, and the second time it starts calling.

So the action I take is this: Since each download is performed in a sub-thread, I'm outside (looping inside) waiting for the child thread to call and then download the next image.

The following is the complete code in my section

list<map<string, object>> data =NewArraylist<map<string, object>> ();//initialization, simpleadapter the data that needs to be bound is the list<map<string,object>> type                         for(inti = 0; i < bitmap.length; i++) {                            Finalmap<string, object> item =NewHashmap<string, object>();    C.setvalue (i); Because I need to pass the I variable to the thread, it gets through the variables of a classThread T_=NewThread (NewRunnable () {@Override Public voidrun () {String url= "/http" +staticvalue.add_ip+ ": 8089/images/" +image_url[C.getvalue ()]; Network URLBitmap[c.getvalue ()]=gethttpbitmap (URL); //or get URL to bitmap method SavePicture (Bitmap[c.getvalue ()],image_url[c.getvalue ()]); //download to local                                }                            });                            T_.start (); Try{ t_.join (); Wait for thread execution to end }Catch(interruptedexception e) {e.printstacktrace (); } FileInputStream FIS=NULL; Try{FIS=NewFileInputStream (Environment.getexternalstoragedirectory (). GetAbsolutePath () 
    + "/m2_test/download/" +Image_url[i]); //Get a picture of a native in just a child thread } Catch(FileNotFoundException e) {e.printstacktrace (); } bitmapfactory.options op=Newbitmapfactory.options (); Op.inpreferredconfig=Bitmap.Config.RGB_565; Op.indither=true; Op.insamplesize= 10; Op.injustdecodebounds=false; Bitmap Bitmap= Bitmapfactory.decodestream (FIS,NULL, op); Item.put ("Pic", bitmap); Item.put ("Mono", Mono[c.getvalue ()]); Data.add (item);                        }
              //simpleadapter default does not support picture acceptance, so you need to override the method Simpleadapter Adapter
=NewSimpleadapter (GetContext (), data, R.layout.vlist,Newstring[]{"pic", "Mono"},New int[]{r.id.im, R.id.tv_mono}]; Adapter.setviewbinder (NewSimpleadapter.viewbinder () { Public Booleansetviewvalue (view view, Object attentionlist, String textrepresentation) {if(ViewinstanceofImageView && AttentionlistinstanceofBitmap) {ImageView IV=(ImageView) view; Iv.setimagebitmap ((Bitmap) attentionlist); return true; }Else{ return false; } } }); Lv.setadapter (adapter);

 PublicBitmap gethttpbitmap (String url) {Bitmap Bitmap=NULL; Try{URL Pictureurl=Newurl (URL); InputStream in=Pictureurl.openstream (); Bitmap=Bitmapfactory.decodestream (in);        In.close (); } Catch(malformedurlexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }        returnbitmap; }     Public voidsavepicture (Bitmap bitmap,string pic_name) {String picturename=environment.getexternalstoragedirectory (). GetAbsolutePath ()+ "/m2_test/download/" +Pic_name; File File=NewFile (picturename);        FileOutputStream out; Try{ out=Newfileoutputstream (file); Bitmap.compress (Bitmap.CompressFormat.JPEG,100, out);            Out.flush ();        Out.close (); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }    }

The above is the code I implemented in the ListView implementation to view the network picture

Android questions about displaying network pictures in the ListView

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.