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 display pictures, I hope this article is helpful to Android developers.
Android gets a directory slice in SDcard