1

I am trying to delete image from device by using file.delete(). Delete method is being called and even it is returning true but when I open my gallery image is still there.

My image path is :

/data/user/0/com.myappname/app_myappname/20180413__164222_0.7680390806195996.png

Please have a look what i have tried.

1)

 File file = new File(path);
        if (file.exists()) {
            file.delete();
        }

2)

try {
 File file = new File("file://" + path);
        String getDirectoryPath = file.getParent(); // Only return path if physical file exist else return null
        File fileNew = new File(getDirectoryPath);
        fileNew.delete();
    } catch (Exception e) {
    }

3)

 File file = mContext.getFilesDir(); // this will get you internal directory path
            Log.d("BLA BLA", file.getAbsolutePath());
            File newfile = new File(file.getAbsolutePath() + imagePath); // foo is the directory 2 create
            newfile.delete();

4)

try
            {
                File sd = Environment.getExternalStorageDirectory();
                File data = Environment.getDataDirectory();

                if (sd.canWrite()) {
                    File fileName = new File(data, imagePath);
                    if (fileName.exists())
                    {
                        fileName.delete();
                    }
                }
            } catch (Exception e) {
            }

I have applied almost every solution from stack overflow related to file.delete(). But nothing is working ? Any help or clue will be appreciated.

Edit:

I have refreshed the gallery when delete() returns true using this method but still image is present in app gallery.

Look at my code:

  public void callBroadCast() {
        if (Build.VERSION.SDK_INT >= 14) {
            Log.e("-->", " >= 14");
            MediaScannerConnection.scanFile(mContext, 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");
            mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                    Uri.parse("file://" + Environment.getExternalStorageDirectory())));
        }
    }
piet.t
  • 11,718
  • 21
  • 43
  • 52
Däñish Shärmà
  • 2,891
  • 2
  • 25
  • 43
  • "when i open my gallery image is still there" -- it will take a while for the `MediaStore` to detect the deleted file and update its index. Use other tools, such as Android Studio's Device File Explorer, to see if you are actually deleting your file. – CommonsWare Apr 13 '18 at 11:34
  • even i restarted my emulator after 10 minutes. Image was still there. – Däñish Shärmà Apr 13 '18 at 11:35
  • verify if the `imagePath` variable is the exact same as your image you want to delete. If they are the same, then you might need to force MediaStore to refresh. You can use this app to force it: https://play.google.com/store/apps/details?id=com.house.noranuko.mediarescanner – Zun Apr 13 '18 at 11:36
  • Using this path i am creating the base64 and saving into local database. This path is working fine to convert image to base64. But when i delete it return true but still image is not deleted but just returns true. – Däñish Shärmà Apr 13 '18 at 11:38
  • Possible duplicate of [android : deleting an image](https://stackoverflow.com/questions/10716642/android-deleting-an-image) – Sachin Rajput Apr 13 '18 at 11:38
  • have you set the read write permissions in the manifest ?? – Mohammad Apr 13 '18 at 11:41
  • @Thunder i have tried your solution and called broadcast but still image is present in gallery. – Däñish Shärmà Apr 13 '18 at 11:42
  • inside your app Gallery ? or in Local storage – Sachin Rajput Apr 13 '18 at 11:43
  • in App gallery. – Däñish Shärmà Apr 13 '18 at 11:43
  • In your code, re-execute the `file.exists()` test after trying to delete the file. Report both the before and after status, as well as the result of the delete attempt. This should be a better indication of whether the file _has_ been deleted than looking in the gallery. – TripeHound Apr 13 '18 at 11:48
  • if you delete a file and then want to refresh your in app gallery as well , there is the answer in mentioned link above – Sachin Rajput Apr 13 '18 at 11:49
  • @TripeHound Yes i tried that too already. I wrote file.delete() two times. First time it returns true and second time it returns false. But in gallery image was not deleted. – Däñish Shärmà Apr 13 '18 at 11:51
  • @Thunder Yes i have used that broadcast but did not work. I have edited my question. – Däñish Shärmà Apr 13 '18 at 11:55
  • Use other tools, such as Android Studio's Device File Explorer, to see if you are actually deleting your file. – CommonsWare Apr 13 '18 at 11:56
  • Are you seeing the _actual image_ in the gallery, or a thumbnail of it? – TripeHound Apr 13 '18 at 11:56
  • @CommonsWare I have tested this in Samsung galaxy J5. Image is saving in My Files/Device Storage/Pictures. – Däñish Shärmà Apr 13 '18 at 12:01
  • @DäñishShärmà Have you tried deleting image using contentResolver? – Sagar Apr 13 '18 at 12:02
  • After spending long time i found what was the issue in my case. I have written this line somewhere in code and it was generating the image in Device Storage/Pictures. Issue was: MediaStore.Images.Media.insertImage(getContentResolver(), mBitmap, AppConstant.TITLE, null); – Däñish Shärmà Apr 13 '18 at 14:38

0 Answers0