Refresh the Gallery after deleting an image file?
Asked Answered
K

3

10

I always found the following answer for my Question:

context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
            + Environment.getExternalStorageDirectory())));

but it do not work on my System (Nexus4 Android 4. ...)

I can create a File and add it to the Media-DB whith this code

Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    Uri contentUri = Uri.fromFile(file);
    mediaScanIntent.setData(contentUri);
    context.sendBroadcast(mediaScanIntent);

Where "file" is the new image-file i want to add.

after deleting the File I try to refresch the gallery by

Intent intent = new Intent(Intent.ACTION_MEDIA_MOUNTED);
    Uri contentUri = Uri.parse("file://" + Environment.getExternalStorageDirectory());
    intent.setData(contentUri);
    context.sendBroadcast(intent);

or

context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
            + Environment.getExternalStorageDirectory()))); 

but there are still empty placeholder in the Galery.

I do not know why?...

To be on the safe side I add too my Activity in the AndroidManifest.xml

<intent-filter>
            <action android:name="android.intent.action.MEDIA_MOUNTED" />
            <data android:scheme="file" /> 
        </intent-filter>

but the result is the same. Any idea to solve the problem?

Kampmeier answered 18/3, 2013 at 21:36 Comment(1)
Possible duplicate of https://mcmap.net/q/146512/-android-deleting-an-imageAnastasiaanastasie
J
14

After the KitKat you can't send the Intent to run the MediaScanner on whole device's storage, because it is a CPU I\O intensive task and if every single app that download an image or delete one, call that intent battery would drain easily, hence they have decided to block that operation. Here are your options:

Use the old way for pre-KitKat

Pass your filePath:

if(Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
    mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                Uri.parse("file://" + Environment.getExternalStorageDirectory())));
} else{


    MediaScannerConnection.scanFile(mContext,  filePath, null, new MediaScannerConnection.OnScanCompletedListener() {
        /*
         *   (non-Javadoc)
         * @see android.media.MediaScannerConnection.OnScanCompletedListener#onScanCompleted(java.lang.String, android.net.Uri)
         */
        public void onScanCompleted(String path, Uri uri) 
        {
        Log.i("ExternalStorage", "Scanned " + path + ":");
        Log.i("ExternalStorage", "-> uri=" + uri);
        }
    });

}

A more reliable approach is to update the MediaStore directly:

// Set up the projection (we only need the ID)
String[] projection = { MediaStore.Images.Media._ID };

// Match on the file path
String selection = MediaStore.Images.Media.DATA + " = ?";
String[] selectionArgs = new String[] { file.getAbsolutePath() };

// Query for the ID of the media matching the file path
Uri queryUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
ContentResolver contentResolver = getContentResolver();
Cursor c = contentResolver.query(queryUri, projection, selection, selectionArgs, null);
if (c.moveToFirst()) {
    // We found the ID. Deleting the item via the content provider will also remove the file
    long id = c.getLong(c.getColumnIndexOrThrow(MediaStore.Images.Media._ID));
    Uri deleteUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
    contentResolver.delete(deleteUri, null, null);
} else {
    // File not found in media store DB
}
c.close();
Jurist answered 23/1, 2015 at 9:10 Comment(0)
J
8

Check below code snippet to verify all the cases for add/delete/move image file programmatically and intimate the gallery app to refresh the data

/***
 * Refresh Gallery after add image file programmatically 
 * Refresh Gallery after move image file programmatically 
 * Refresh Gallery after delete image file programmatically
 * 
 * @param fileUri : Image file path which add/move/delete from physical location
 */
public void refreshGallery(String fileUri) {

    // Convert to file Object
    File file = new File(fileUri);

    if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
        // Write Kitkat version specific code for add entry to gallery database
        // Check for file existence
        if (file.exists()) {
            // Add / Move File
            Intent mediaScanIntent = new Intent(
                    Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            Uri contentUri = Uri.fromFile(new File(fileUri));
            mediaScanIntent.setData(contentUri);
            BaseApplication.appContext.sendBroadcast(mediaScanIntent);
        } else {
            // Delete File
            try {
                BaseApplication.appContext.getContentResolver().delete(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        MediaStore.Images.Media.DATA + "='"
                                + new File(fileUri).getPath() + "'", null);
            } catch (Exception e) {
                e.printStackTrace();

            }
        }
    } else {
        BaseApplication.appContext.sendBroadcast(new Intent(
                Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
                        + getBaseFolder().getAbsolutePath())));
    }
}
Joscelin answered 21/1, 2015 at 5:19 Comment(0)
N
0

For Xamarin C# you can Use following Code!

Just pass the full File path to the Array

 Android.Media.MediaScannerConnection.ScanFile(Android.App.Application.Context, new string[] { deletedImageFilePath}, null, null); 
Noncooperation answered 31/12, 2020 at 19:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.