Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android : deleting an image

I am deleting an image file from my application. I was doing

new  File(filename).delete (); 

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

On search i found that we should use

getContentResolver().delete(Uri.fromFile(file), null,null); to delete

But here i am getting the 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 to fix this issue. Is there any other way to update gallery when image is physically deleted

like image 453
png Avatar asked May 23 '12 08:05

png


People also ask

Why can't I delete photos on my Android?

Go to “Settings” > “Accounts” > “Google“. From there, you can select the Google account you are using, then uncheck the “Sync Picasa Web Albums” option. Now under “Settings” > “Application manager“, swipe over to “All” > “Gallery“, and select “Clear data“.

How do I delete a photo from the gallery?

Delete Single Photo Open the “Gallery” or “Photos” app. Open the album that contains the photo you wish to remove. Tap and hold the photo until a trash icon appears at the upper-right portion of the screen. Tap the “Trash” icon located at the upper-right portion of the screen.

Does Android have a deleted picture folder?

Does Android have a recently deleted folder? No, there's no recently deleted folder like on iOS. When Android users delete photos and images, they can't get them back unless they have a backup or use a third-party photo recovery application like Disk Drill for Mac.

Why wont my phone let me delete pictures?

The chances are that the iPhone won't delete photos because you have previously synced your photos with iTunes or added photos to the device with a third-party app. Most likely the photos you can't delete are located in the From My Mac section of the Photos app.


1 Answers

Use the code below, it 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 refresh gallery after deleting image use below code for send Broadcast

(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 

is 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 is 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 
like image 106
Dhaval Parmar Avatar answered Oct 15 '22 13:10

Dhaval Parmar