Android之操作相册

获取手机中的图片的绝对路径并且区分出每个文件夹下的路径:

存放图片绝对路径的文件夹的名字和存放绝对路径的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     

未完....

原文地址:https://www.cnblogs.com/shiguotao-com/p/5170308.html