Yuva K
Yuva K

Reputation: 131

how to get gallery folder path?

I'm trying to delete an image using this function. This function deletes the image which was last modified(or the recent one).

public void deleteImage() {

        File f = new File(getGalleryPath());

        File [] files = f.listFiles();

        Arrays.sort(files, new Comparator<File>() {
            @Override
            public int compare(File a, File b) {
                if (a.lastModified() < b.lastModified())
                    return 1;
                if (a.lastModified() > b.lastModified())
                    return -1;
                return 0;
            }
        });

        files[0].delete();
    }

I want to get path of the gallery and I tried this function :

private static String getGalleryPath() {
        return  Environment.getExternalStorageDirectory() + "/" + Environment.DIRECTORY_DCIM + "/";
    }

This function fails to get the appropriate path of the gallery when the gallery is in SD card. How can I get the path here regardless of gallery's location?

UPDATE :

I'm able to delete images but in devices using Lollipop, the images are still there. However, images are grayed out and their size is 0 kb. Here is the code I'm using :

public File getAlbumStorageDir(String albumName) {
// Get the directory for the user's public pictures directory.
File file = new File(Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_PICTURES), albumName);
if (!file.mkdirs()) {
    Log.e(LOG_TAG, "Directory not created");
}
return file;

I have tested this code in two Xiaomi phones using API >= 24. However, when I tested it in LG G3 Beat (API 21), the images were still there but grayed out. Please let me know if someone knows a fix here.

Upvotes: 1

Views: 3109

Answers (2)

Kyle R
Kyle R

Reputation: 109

The preferred method of interacting with media files on External Storage in newer versions of Android is through the MediaStore. The idea of this is to abstract away the necessity to access files using a direct path and instead refer to them by their type and ID through a Content Provider which indexes the files (such as the MediaStore). It is also becoming more complicated to use the methods you've mentioned because of security changes to file access on Android.

To get the most recently modified file, you will want to use something like this

//Type of Data to return for each image
String[] retColumns = { MediaStore.Images.Media.DATE_MODIFIED,
                        MediaStore.Images.Media.SIZE,
                        MediaStore.Images.Media._ID,
                        MediaStore.Images.Media.TITLE,
                        MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
                        MediaStore.Images.Media.BUCKET_ID};

//How to order the results
String orderBy = MediaStore.Images.Media.DATE_MODIFIED;

Cursor imageCursor;

//Only return files where the selection is equal to the selectionArgs
String selection = MediaStore.Images.Media.RELATIVE_PATH + "=?";
String[] selectionArgs = {"Relative Path of Folder like DCIM/Instagram"};
//You can get this from an image you already know the ID of 
//by accessing the MediaStore.Images.Media.RELATIVE_PATH column

imageCursor = ctx.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, retColumns, selection, selectionArgs, orderBy);
imageCursor.moveToPosition(imageCursor.getCount() - 1)

Since the data we returned was ordered by date modified, the last file should be the one that was modified last. You can read data fields from the file by using the associated cursor commands, for example (note: only columns that were returned in retColumns above)

Long dateModified = imageCursor.getLong(imageCursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED));

int imageID = imageCursor.getInt(imageCursor.getColumnIndex(MediaStore.Images.Media._ID));

You can then delete said file using the following

ctx.getContentResolver().delete(Uri.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageID), null, null);

On versions less than 29, the WRITE_EXTERNAL_STORAGE permission will be enough. On Versions 29+ this will throw a RecoverableSecurityException that will need to be caught and handled at runtime, as these types of permissions are no longer guaranteed.

Upvotes: 1

emandt
emandt

Reputation: 2706

public File getAlbumStorageDir(String albumName) {
// Get the directory for the user's public pictures directory.
File file = new File(Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_PICTURES), albumName);
if (!file.mkdirs()) {
    Log.e(LOG_TAG, "Directory not created");
}
return file;

Upvotes: 0

Related Questions