Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android file delete leaves empty placeholder in Gallery

I insert an image via:

    ContentValues values = new ContentValues();   
    values.put(Images.Media.TITLE, filename);
    values.put(Images.Media.DATE_ADDED, System.currentTimeMillis()); 
    values.put(Images.Media.MIME_TYPE, "image/jpeg");

    Uri uri = this.getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);

But when I try to delete

    File f = new File(imageURI);
    f.delete();

The picture is no longer there but an empty placeholder is. Any ideas?

like image 268
Paul Avatar asked Dec 13 '10 16:12

Paul


2 Answers

Android has a cache of sorts that keeps track of media files.

Try this:

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

It makes the MediaScanner service run again, which should remove the deleted image from the device's cache.

Looks like you also need to add this permission to your AndroidManifest.xml:

<intent-filter>
  <action android:name="android.intent.action.MEDIA_MOUNTED" />
  <data android:scheme="file" /> 
</intent-filter>
like image 185
Marc Bernstein Avatar answered Oct 06 '22 01:10

Marc Bernstein


As stated in the comments, the accepted answer uses a lot of memory. While MediaScannerConnection doesn't have a "deleteFile" method, just pass in the the old file path to the "scanFile" method once you've deleted the file. The media scanner will rescan and remove the media.

tested on N5. Android 4.4.

EDIT: Other have stated this doesn't work on 4.2

new AsyncTask<Void, Void, Void>(){
        @Override
        protected Void doInBackground(Void... params) {
            String filePath = recording.file.getAbsolutePath();
            recording.file.delete();
            MediaScannerConnection.scanFile(context,
                    new String[]{filePath}, null, null);
            return null;
        }
    }.execute();
like image 43
whizzle Avatar answered Oct 06 '22 01:10

whizzle