Android: delete image

I am removing the image file from my application. I was doing

new File(filename).delete (); 

This actually deleted the file. But the image was still visible in the gallery.

In the search, I found that we should use

getContentResolver().delete(Uri.fromFile(file), null,null); for removing

But here I get an exception:

Unknown file url. java.lang.IllegalArgumentException: Unknown URL File: ///mnt/sdcard/DCIM/Camera/IMG_20120523_122612.jpg

When I see with any file browser, this particular image is present. Please help me fix this problem. Is there any other way to update the gallery when physically deleting the image.

+30
android image gallery
May 23 '12 at 8:56
source share
9 answers

Use the code below, this may help you.

 File fdelete = new File(file_dj_path); if (fdelete.exists()) { if (fdelete.delete()) { System.out.println("file Deleted :" + file_dj_path); } else { System.out.println("file not Deleted :" + file_dj_path); } } 



to update the gallery after deleting the image using the code below to send broadcasts

(for <KITKAT API 14)

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

For> = KITKAT API 14 use below code

 MediaScannerConnection.scanFile(this, new String[] { Environment.getExternalStorageDirectory().toString() }, 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); } }); 

Because:

 ACTION_MEDIA_MOUNTED 

deprecated in KITKAT (API 14).




EDITED 04-09-2015

its working fine check below code

 public void deleteImage() { String file_dj_path = Environment.getExternalStorageDirectory() + "/ECP_Screenshots/abc.jpg"; File fdelete = new File(file_dj_path); if (fdelete.exists()) { if (fdelete.delete()) { Log.e("-->", "file Deleted :" + file_dj_path); callBroadCast(); } else { Log.e("-->", "file not Deleted :" + file_dj_path); } } } public void callBroadCast() { if (Build.VERSION.SDK_INT >= 14) { Log.e("-->", " >= 14"); MediaScannerConnection.scanFile(this, new String[]{Environment.getExternalStorageDirectory().toString()}, 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.e("ExternalStorage", "Scanned " + path + ":"); Log.e("ExternalStorage", "-> uri=" + uri); } }); } else { Log.e("-->", " < 14"); sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()))); } } 

below - logs

 09-04 14:27:11.085 8290-8290/com.example.sampleforwear E/-->﹕ file Deleted :/storage/emulated/0/ECP_Screenshots/abc.jpg 09-04 14:27:11.085 8290-8290/com.example.sampleforwear E/-->﹕ >= 14 09-04 14:27:11.152 8290-8290/com.example.sampleforwear E/﹕ appName=com.example.sampleforwear, acAppName=/system/bin/surfaceflinger 09-04 14:27:11.152 8290-8290/com.example.sampleforwear E/﹕ 0 09-04 14:27:15.249 8290-8302/com.example.sampleforwear E/ExternalStorage﹕ Scanned /storage/emulated/0: 09-04 14:27:15.249 8290-8302/com.example.sampleforwear E/ExternalStorage﹕ -> uri=content://media/external/file/2416 
+31
May 23 '12 at 9:08 a.m.
source share

I saw many answers suggesting using

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

This works, but causes the Media Scanner to rescan the media on the device. A more efficient approach would be to request / delete through the Media Store content provider:

 // 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(); 
+34
Dec 26 '13 at 6:51
source share
 File file = new File(photoUri); file.delete(); context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(photoUri)))); 

This code works for me, and I think it's better than reloading the entire SD card using Intent.ACTION_MEDIA_MOUNTED

+23
Sep 23 '14 at 9:36
source share

To delete an image,

 ContentResolver contentResolver = getContentResolver(); contentResolver.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, MediaStore.Images.ImageColumns.DATA + "=?" , new String[]{ imagePath }); 
+14
May 22, '15 at 17:45
source share

I tried all these solutions but no luck in Android 6.
In the end, I found this sketchy code that worked fine.

 public static void deleteFileFromMediaStore(final ContentResolver contentResolver, final File file) { String canonicalPath; try { canonicalPath = file.getCanonicalPath(); } catch (IOException e) { canonicalPath = file.getAbsolutePath(); } final Uri uri = MediaStore.Files.getContentUri("external"); final int result = contentResolver.delete(uri, MediaStore.Files.FileColumns.DATA + "=?", new String[]{canonicalPath}); if (result == 0) { final String absolutePath = file.getAbsolutePath(); if (!absolutePath.equals(canonicalPath)) { contentResolver.delete(uri, MediaStore.Files.FileColumns.DATA + "=?", new String[]{absolutePath}); } } } 

I also tested this on Android 4.4 and 5.1 and it works great.

+5
Mar 29 '16 at 23:31
source share
 sendBroadcast(new Intent( Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()))); 

This code works, but it is very expensive. It unmounts and then mounts the SDCard, which can affect some applications or use huge system resources to update the gallery. I'm still looking for a better alternative and will post if I get it.

+4
Sep 22 '12 at 13:45
source share

In Kotlin you can do this:

 private fun deleteImage(path: String) { val fDelete = File(path) if (fDelete.exists()) { if (fDelete.delete()) { MediaScannerConnection.scanFile(this, arrayOf(Environment.getExternalStorageDirectory().toString()), null) { path, uri -> Log.d("debug", "DONE") } } } } 
0
Sep 06 '18 at 8:37
source share

I had the same problem and I tried three different methods to delete the image. Sometimes it worked, sometimes it is not. After too much time spent on every method that I have, the image will be deleted. What I want to say: BE CAREFUL WITH BITMAP PROCESSING . I took a picture, saving it, and then rotate, if necessary:

 public static Bitmap rotatePictureToPortraitMode(String filePath, Bitmap myBitmap) { try { ExifInterface exif = new ExifInterface(filePath); int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1); Log.d("EXIF", "Exif: " + orientation); Matrix matrix = new Matrix(); if (orientation == 6) { matrix.postRotate(90); } else if (orientation == 3) { matrix.postRotate(180); } else if (orientation == 8) { matrix.postRotate(270); } myBitmap = Bitmap.createBitmap(myBitmap, 0, 0, myBitmap.getWidth(), myBitmap.getHeight(), matrix, true); // rotating bitmap } catch (Exception e) { } return myBitmap; } 

after that I tried to delete the image, but as I said earlier, it did not work. Removing this method helped me solve the problem.

Maybe this was just my problem, but as soon as I deleted it, it helped me a lot, so I want to say how you process the image. In my case, I used the previously mentioned answer:

 File file = new File(photoUri); file.delete(); context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(photoUri)))); 

Hope it helps!

0
Dec 6 '18 at 17:28
source share
 public static boolean deltefolderwithimages(File dir) { if (dir.isDirectory()) { String[] children = dir.list(); for (int i=0; i<children.length; i++) { boolean success = deltefolderwithimages(new File(dir, children[i])); if (!success) { return false; } } } return dir.delete(); } 
0
Aug 28 '19 at 7:22
source share



All Articles