Android get image resources for SD card

Source: Internet
Author: User

First I get the root directory path under the SD card:

privatestring Issdcard () {      File sdcarddir=null;      Boolean issdexist=environment.getexternalstoragestate (). Equals (environment.media_mounted);       if (issdexist) {        // If there is sdcard, locate the directory   sdcarddir= Environment.getexternalstoragedirectory (); return sdcarddir.tostring ();       }else  {        returnnull;}}           }

After finding the/sdcard, start using file[] f = file.listfile () to get a list of all the files in the directory.

 Public voidGetpicpath (String sdpath) {//Open the SD card directoryFile File=NewFile (Sdpath); //get the SD card directory listfile[] Files=File.listfiles ();  for(intz=0; z<files.length;z++) {File F=Files[z]; if(F.isfile ()) {isfile (f); }Else{notfile (f); }        }}

In the case of a file, jump into the isfile, or in the method, if it is a folder, jump into the notfile (file file);

 Public voidisfile (file file) {log.i (TAG,"Isfile"); String FNM=File.getpath (); LOG.I (TAG,"isfile=="+FNM); MAPSD=NewHashmap<string, string>(); String filename=File.getname (); intIDX = Filename.lastindexof ("."); if(IDX <=0) {            return; } String suffix=filename.substring (idx+1, Filename.length ()); if(Suffix.tolowercase (). Equals ("jpg") ||suffix.tolowercase (). Equals ("JPEG") ||suffix.tolowercase (). Equals ("BMP") ||suffix.tolowercase (). Equals ("PNG") ||suffix.tolowercase (). Equals (". gif") {Mapsd.put ("ImagePath", File.getpath (). toString ());        Listsd.add (MAPSD); }   }

If there is a file isfile to match his filename suffix is a picture, then put into the MAPSD, and then add to the list.

If a file is a folder, continue to use File.listfile () to open the contents of its folder, and then use the For statement to determine if there is a file in it, if there is isfile (), No, use Getpicpath () to open it.

 Public voidnotfile (file file) {log.i (TAG,"notfile Yes"); LOG.I (TAG,"Notfilepath"+File.getpath ()); file[] Files=File.listfiles (); if(Files = =NULL){          return; }         for(inti =0; i<files.length;i++) {log.i (TAG,"notfile int="+string.valueof (files.length)); File FIS=Files[i]; if(Fis.isfile ()) {isfile (FIS); }Else{String Sdpath=Fis.getpath (); File filesd=NewFile (Sdpath); LOG.I (TAG,"Notfile ="+filesd); File[] Filess=Filesd.listfiles (); if(Filess = =NULL){              return; }            for(intj=0; j<filess.length;j++) {Getpicpath (filesd.tostring ()); }          }        }    }

After these complicated transformations, we can finally get the existenceSdcaredUnder all the picturesPathOfList, but this one.ListBut it's great, because it's even. thumbnailsUnder the directory picture also got, actually at first I don't know what this file came from, insdcare I can't see it, and then a translator knows it's a thumbnail. Re-read the network, found that the thumbnail can actually cursor Look, again, the thumbnail and the original picture should be related, sure enough, can be thumbnails.image_id,media.external_content_uri, path Finally, it was just two simple ways to get sd path But the thumbnails we get are the entire Card picture thumbnail, how to put them in different folders to split it? I'm still studying.

Get thumbnails First:

List =NewArraylist(); CR=Getcontentresolver (); string[] Projection={thumbnails._id, thumbnails.image_id, thumbnails.data}; Cursor Cursor=cr.query (Thumbnails.external_content_uri, projection,NULL,NULL,NULL);if(Cur.movetofirst ()) {int_id; intimage_id;        String Image_path; int_idcolumn =Cur.getcolumnindex (thumbnails._id); intImage_idcolumn =Cur.getcolumnindex (thumbnails.image_id); intDataColumn =Cur.getcolumnindex (Thumbnails.data);        LOG.I (TAG, string.valueof (Image_idcolumn));  Do        {           //Get the field values_id=Cur.getint (_idcolumn); image_id=Cur.getint (Image_idcolumn); Image_path=cur.getstring (DataColumn); //Do something with the values. //log.i (TAG, _id + "image_id:" + image_id + "path:"//+ image_path + "---");HashMap<string, string> hash =NewHashmap<string, string>(); Hash.put ("image_id", image_id +""); Hash.put ("Path", Image_path);        List.add (hash); }         while(Cur.movetonext ());} The above method obtains the list that contains Image_path and ID here the image_path is actually the path of the thumbnail, to find the path of the original image to use the relationship between image_id and the original path to find, as follows: I am using simpleadapter to display pictures, So click on the time will get a position position int, and then find image_id; Public voidOnitemclick (adapterview<?> arg0, VIEWARG1,intposition,LongArg3) {CR=Getcontentresolver (); String image_id= list.Get(position).Get("image_id"); string[] Projection={media._id, media.data}; Cursor Cursor=cr.query (Media.external_content_uri, projection, media._id+"="+ image_id,NULL,NULL); if(Cursor! =NULL) {Cursor.movetofirst (); Stringpath=cursor.getstring (Cursor.getcolumnindex (media.data)); LOG.I (Tag,path);}Else{Toast.maketext ( This,"Image doesn ' t exist!", Toast.length_short). Show (); }  }

Android get image resources for SD card

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.