3

I'm making an app with a feature to allow images to be uploaded to the web server. After the captured image is uploaded to server, the image will be deleted from the user's device. I can upload the image and after I check the file manager, it is already removed. But somehow when I open the gallery, it's still there although it shows "unable to load file". But when I check details, it shows the size of the image. So how can I totally removed the uploaded image from the file manager & the gallery?

This is my code :

Public Static Boolean deleteDirectory(File path) {
        // TODO Auto-generated method stub
        if( path.exists() ) {
            File files = path;

                if(files.isDirectory()) {
                    deleteDirectory(files);
                }
                else {
                    files.delete();
                }
            }

        return(path.delete());
    }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Nico Pratama
  • 31
  • 1
  • 2
  • possible duplicate of [android : deleting an image](http://stackoverflow.com/questions/10716642/android-deleting-an-image) – Dhaval Parmar Aug 11 '15 at 07:57

2 Answers2

5

You have to update (scan) your media content provider. I use this method to do both deleting and scanning the media content, although you will have to do some changes.

private void DeleteAndScanFile(final Context context, String path,
        final File fi) {
    String fpath = path.substring(path.lastIndexOf("/") + 1);
    Log.i("fpath", fpath);
    try {
        MediaScannerConnection.scanFile(context, new String[] { Environment
                .getExternalStorageDirectory().toString()
                + "/images/"
                + fpath.toString() }, null,
                new MediaScannerConnection.OnScanCompletedListener() {
                    public void onScanCompleted(String path, Uri uri) {
                        if (uri != null) {
                            context.getContentResolver().delete(uri, null,
                                    null);
                        }
                        fi.delete();
                        System.out.println("file Deleted :" + fi.getPath());
                        Log.i("ExternalStorage", "Scanned " + path + ":");
                        Log.i("ExternalStorage", "-> uri=" + uri);
                    }
                });
    } catch (Exception e) {
        e.printStackTrace();
    }

}
Alamri
  • 2,112
  • 2
  • 16
  • 21
3

Please Try This Way,This Code Worked For Me.

refreshGallery(this, videoPath);

.

public static void refreshGallery(Context context, String path) {
    File file = new File(path);
    if (file.exists() && file.isFile()) {
        file.delete();
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        Intent intent= new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        intent.setData(Uri.fromFile(file));
        context.sendBroadcast(intent);
    } else {
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse(file.getAbsolutePath())));
    }
}
Mihir Akoliya
  • 166
  • 1
  • 7