Path to Android folder with all images

坚强是说给别人听的谎言 提交于 2019-12-13 03:53:49

问题


I'm trying to build a feature to load the latest saved image from the device into an app. Currently I'm using this path DCIM/Camera/ but it only stores camera taken images. Is there a folder that stores all images? (screenshots, images saved from the web or camera roll)


回答1:


Instead of searching in in folders for the images, you can let the system take care of that for you.

Android indexes most media-data (images, music, etc) on it's own and offers Content Providers to query these databases.

For your purposes, you can use the MediaStore.Images.Media-provider.

public List<String> getImagePaths(Context context) {
    // The list of columns we're interested in:
    String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_ADDED};

    final Cursor cursor = context.getContentResolver().
            query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, // Specify the provider
                    columns, // The columns we're interested in
                    null, // A WHERE-filter query
                    null, // The arguments for the filter-query
                    MediaStore.Images.Media.DATE_ADDED + " DESC" // Order the results, newest first
            );

    List<String> result = new ArrayList<String>(cursor.getCount());

    if (cursor.moveToFirst()) {
        final int image_path_col = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        do {
            result.add(cursor.getString(image_path_col));
        } while (cursor.moveToNext());
    }
    cursor.close();

    return result;
}

The method will return a list of the image-paths of all images that are currently indexed by the MediaStore with the latest first. You'll need the android.permission.READ_EXTERNAL_STORAGE-permission for this to work!



来源:https://stackoverflow.com/questions/30425066/path-to-android-folder-with-all-images

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!