Reputation: 152
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)
Upvotes: 0
Views: 3049
Reputation: 25755
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!
Upvotes: 3