This article describes how to get all the images in a directory of SDcard in Android development and show them, the following we provide this function is generic, as long as the path can be provided to query the directory of all images of the path information, and saved to a list<string>.
1. Get a collection of directory slices in SDcard
PublicList<string>getpictures (Final String strpath) {List<String> list =NewArraylist<string>(); File File=NewFile (strpath); File[] Allfiles=File.listfiles (); if(Allfiles = =NULL) { return NULL; } for(intK =0; K < Allfiles.length; k++) {Final File fi=Files[i]; if(Fi.isfile ()) {intIDX = Fi.getpath (). LastIndexOf ("."); if(IDX <=0) { Continue; } String suffix=Fi.getpath (). substring (IDX); if(Suffix.tolowercase (). Equals (". jpg") ||suffix.tolowercase (). Equals (". JPEG") ||suffix.tolowercase (). Equals (". bmp") ||suffix.tolowercase (). Equals (". PNG") ||suffix.tolowercase (). Equals (". gif") {List.add (Fi.getpath ()); } } } returnlist;}
2. Get the image under the SD card and show
list<string> list = Getpictures (Environment.getexternalstoragedirectory () +""); if(List! =NULL) {log.d (TAG,"list.size ="+list.size ()); for(inti =0; I < list.size (); i++) {Bitmap BM= Bitmapfactory.decodefile (list.Get(i)); inttop = -; if(I >0) {Top+ = Bitmapfactory.decodefile (list.GetI1). GetHeight () +2; } canvas.drawbitmap (BM,0, top, paint); } } Else{log.d (TAG,"list is NULL!!!"); }
This article provides two functions, the first function is to get all the pictures in a directory in SDcard, the second function is mainly to show the graph
Saving a picture can be encapsulated as a class:
Package Com.mogunote.adapter;import Java.io.file;import java.io.filenotfoundexception;import Java.io.fileoutputstream;import Android.graphics.bitmap;import Android.graphics.bitmap.compressformat;import Android.os.environment;import Android.view.View; Public classSaveviewutil {Private StaticFinal File RootDir =NewFile (Environment.getexternalstoragedirectory () +file.separator+"paints/"); /** Methods of preservation*/ Public StaticBoolean savescreen (view view) {//determine if SDcard is available if(!Environment.MEDIA_MOUNTED.equals (Environment.getexternalstoragestate ())) { return false; } if(!rootdir.exists ()) {Rootdir.mkdir (); } view.setdrawingcacheenabled (true); View.builddrawingcache (); Bitmap Bitmap=View.getdrawingcache (); Try{bitmap.compress (compressformat.jpeg, -,NewFileOutputStream (NewFile (Rootdir,system.currenttimemillis () +". jpg"))); return true; } Catch(FileNotFoundException e) {e.printstacktrace (); return false; }finally{view.setdrawingcacheenabled (false); Bitmap=NULL; } }}
Call Encapsulation class:
if (Saveviewutil.savescreen (Hbview)) { Toast.maketext (this" success " ) 0 ). Show (); } Else { Toast.maketext (this" failed, please check if SDcard is available "0 ). Show (); }
Android gets a directory slice in SDcard