1. 程式人生 > >android獲取手機所有圖片

android獲取手機所有圖片

    public ImageFilesBean returnImageFiles() {
        List<String> parentsPthe = new ArrayList<>();
HashMap<String, String> pathMap = new HashMap<>();
Cursor cursor = getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null
); while (cursor.moveToNext()) { //獲取其他圖片資訊 // String fileName = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME)); String filePath = cursor.getString(cursor .getColumnIndex(MediaStore.Images.Media.DATA)); String parentName = new
File(filePath).getParentFile().getAbsolutePath(); if (!parentsPthe.contains(parentName)) { parentsPthe.add(parentName); pathMap.put(parentName, filePath); } } cursor.close(); return ImageFilesBean.returnBean(parentsPthe, pathMap); }
package beini.com.ddd;
import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class ImageFilesBean { private List<String> parentsPthe = new ArrayList<>(); private HashMap<String, String> pathMap = new HashMap<>(); public List<String> getParentsPthe() { return parentsPthe; } public ImageFilesBean setParentsPthe(List<String> parentsPthe) { this.parentsPthe = parentsPthe; return this; } public HashMap<String, String> getPathMap() { return pathMap; } public ImageFilesBean setPathMap(HashMap<String, String> pathMap) { this.pathMap = pathMap; return this; } public static ImageFilesBean returnBean(List<String> parentsPthe, HashMap<String, String> pathMap) { return new ImageFilesBean().setParentsPthe(parentsPthe).setPathMap(pathMap); } }