Android gets a picture of a directory in SDcard

Source: Internet
Author: User

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

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.