標籤:
擷取手機中的圖片的絕對路徑並且區分出每個檔案夾下的路徑:存放圖片絕對路徑的檔案夾的名字和存放絕對路徑的List 實體類如下:
1 import java.util.ArrayList; 2 import java.util.List; 3 4 public class ImagePath { 5 private String pakagePath; 6 private List<String> listPth = new ArrayList<String>(); 7 8 9 public String getPakagePath() {10 return pakagePath;11 }12 public void setPakagePath(String pakagePath) {13 this.pakagePath = pakagePath;14 }15 public List<String> getListPth() {16 return listPth;17 }18 public void setListPth(List<String> listPth) {19 this.listPth = listPth;20 }21 22 }將手機中的圖片絕對路徑按包名區分開:
1 private ContentResolver contentResolver; 2 private Button button; 3 private Uri uri; 4 private List<ImagePath> imagePaths; 5 private List<ImagePath> getImgPath() { 6 imagePaths = new ArrayList<ImagePath>(); 7 contentResolver = this.getContentResolver(); 8 uri = Media.EXTERNAL_CONTENT_URI; 9 Cursor cursor = contentResolver.query(uri, null, null, null, null);10 Set<String> set = new TreeSet<String>();11 List<String> lsPsth = new ArrayList<String>();12 while (cursor.moveToNext()) {13 //擷取絕對路徑14 String path = cursor.getString(cursor.getColumnIndex(Media.DATA));15 //將絕對路徑添加到list中16 lsPsth.add(path);17 //擷取包名18 String pakageName = cursor.getString(cursor.getColumnIndex(Media.BUCKET_DISPLAY_NAME));19 //將包名添加到set中20 set.add(pakageName);21 }22 //set轉換成數組23 String[] str =set.toArray(new String[set.size()]);24 //迴圈裝有包名的數組25 for (int i = 0; i < str.length; i++) {26 //建立一個ImagePath對象,用於儲存某個包下的所有絕對路徑(包含包名)27 ImagePath imagePath = new ImagePath();28 //建立一個ImagePath對象,用於儲存某個包下的所有絕對路徑(不包含包名)29 List<String> listpath = new ArrayList<String>();30 //將str數組中的第一個包名存入到imagePath對象中31 imagePath.setPakagePath(str[i]);32 //迴圈lsPsth中的所有絕對路徑33 for (int k = 0; k < lsPsth.size(); k++) {34 //判斷lsPathget(k)當前這條絕對路徑是否是str[i]包名下的絕對路徑35 if(getPakageNameByPath(lsPsth.get(k)).equals(str[i])){36 //如果是存入listpath中37 listpath.add(lsPsth.get(k));38 }39 }40 //將區分出來的對局路徑存入到對用的包名的imagePath對象中41 imagePath.setListPth(listpath);42 //將該對象存入imagePaths集合中43 imagePaths.add(imagePath);44 }45 return imagePaths;46 }
1 /** 2 * 根據據對路徑擷取該路徑的包名 3 * @param path 4 * @return 5 */ 6 public String getPakageNameByPath(String path){ 7 String pkgName = null; 8 if(path.length()>0){ 9 String[] str = path.split("/");10 pkgName = str[str.length-2];11 }12 return pkgName;13 }14
未完....
Android之操作相簿