Create an asyncloadedimage class that inherits the asynctask asynchronous loading class and call the publishprogress method to update onprogressupdate to store the thumbnail information to the adapter. Listener adapter change to asynchronously load thumbnails.
Main. xml
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <GridView android:id="@+id/sdcard" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:horizontalSpacing="10dp" android:numColumns="auto_fit" android:stretchMode="columnWidth" android:verticalSpacing="10dp" /></FrameLayout>
Mfile. Java
Public class mfile extends activity {private gridview sdcardimages; private fileadapter; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); requestwindowfeature (window. feature_indeterminate_progress); setcontentview (R. layout. main); setupviews (); setprogressbarindeterminatevisibility (true); New asyncloadedimage(.exe cute ();}/** release bitmap memory */Protected void ondestroy () {super. ondestroy (); Final gridview grid = sdcardimages; Final int COUNT = grid. getchildcount (); imageview v = NULL; For (INT I = 0; I <count; I ++) {v = (imageview) grid. getchildat (I); (bitmapdrawable) v. getdrawable ()). setcallback (null) ;}}/** initialization file view */private void setupviews () {sdcardimages = (gridview) findviewbyid (R. id. sdcard); sdcardimages. setonitemcl Icklistener (New filelistener (); fileadapter = new fileadapter (getapplicationcontext (); sdcardimages. setadapter (fileadapter);}/** refresh adapter */private void addimage (loadedimage... value) {for (loadedimage image: Value) {fileadapter. addphoto (image); fileadapter. notifydatasetchanged () ;}}/** click to listen */class filelistener implements onitemclicklistener {@ override public void onitemclick (adapt Erview <?> Paramadapterview, view paramview, int paramint, long paramlong) {}}/** asynchronously load the thumbnail to loadedimage and call the addimage method to update adapter */class asyncloadedimage extends asynctask <object, loadedimage, object >{@ override protected object doinbackground (object... params) {string Path = environment. getexternalstoragedirectory (). getabsolutepath () + "/hfdatabase/3/IMG"; file = new file (PATH); If (! File. exists () {file. mkdirs ();} else {file [] files = file. listfiles (); string [] paths = new string [files. length]; Bitmap bitmap; bitmap newbitmap; For (INT I = 0; I <files. length; I ++) {paths [I] = files [I]. getpath (); try {bitmapfactory. options = new bitmapfactory. options (); options. insamplesize = 10; bitmap = bitmapfactory. decodefile (paths [I], options); newbitmap = thumbnailutils. extr Actthumbnail (bitmap, 80, 80); bitmap. Recycle (); If (newbitmap! = NULL) {publishprogress (New loadedimage (newbitmap) ;}} catch (exception e) {e. printstacktrace () ;}} return NULL ;}@ override public void onprogressupdate (loadedimage... value) {addimage (value) ;}@ override protected void onpostexecute (Object result) {setprogressbarindeterminatevisibility (false );}} /** adapter */class fileadapter extends baseadapter {private context mcontext; private arraylist <loadedimage> photos = new arraylist <loadedimage> (); Public fileadapter (context) {mcontext = context;} public void addphoto (loadedimage photo) {photos. add (photo);} public int getcount () {return photos. size ();} public object getitem (INT position) {return photos. get (position);} public long getitemid (INT position) {return position;} public view getview (INT position, view convertview, viewgroup parent) {final imageview; if (convertview = NULL) {imageview = new imageview (mcontext);} else {imageview = (imageview) convertview;} imageview. setscaletype (imageview. scaletype. fit_center); imageview. setpadding (8, 8, 8, 8); imageview. setimagebitmap (photos. get (position ). getbitmap (); Return imageview;}/** This is a class for storing bitmap. Add it to the arraylist of the adapter and update the adapter */Private Static class loadedimage {bitmap mbitmap; loadedimage (Bitmap bitmap) {mbitmap = bitmap;} public bitmap getbitmap () {return mbitmap ;}/ ** message prompt */private Toast toast; Public void showmsg (string Arg) {If (toast = NULL) {toast = toast. maketext (this, ARG, toast. length_short);} else {toast. cancel (); toast. settext (ARG);} toast. show ();}}