Skip to content Skip to sidebar Skip to footer

Refresh Mediastore For Images & Videos

I moving and deleting lots of images and videos as per my requirement and now i am scan media by using Intent scanFileIntent = new Intent( Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, ur

Solution 1:

Use MediaScannerConnection instead.

publicvoidcallScanItent(Context context,String path) {
    MediaScannerConnection.scanFile(context,
            newString[] { path }, null,null);
}

OR

publicvoidcallScanItent(Context context,String path) {
    MediaScannerConnection.scanFile(context,
            newString[]{path}, null, newMediaScannerConnection.OnScanCompletedListener() {
                @OverridepublicvoidonScanCompleted(String path, Uri uri) {
                    Log.d("Scan complete for: ",path);
                }
            });
}

Solution 2:

You should try this solution for amove and delete the image.

Pass file directory path inside deleteRecursive(fileOrDirectory) and you can delete multiple and the single image from file or directory.

publicvoiddeleteRecursive(File fileOrDirectory) {
    if (!fileOrDirectory.isDirectory()) return;
    File[] childFiles = fileOrDirectory.listFiles();
    if (childFiles == null) return;
    if (childFiles.length == 0) {
        fileOrDirectory.delete();
    } else {
        for (File childFile : childFiles) {
            deleteRecursive(childFile);
            DeleteAndScanFile(MainActivity.this, childFile.getPath(), childFile);
        }
    }


}

privatevoidDeleteAndScanFile(final Context context, String path,
                               final File fi) {
    String fpath = path.substring(path.lastIndexOf("/") + 1);
    try {
        MediaScannerConnection.scanFile(context, newString[]{Environment
                        .getExternalStorageDirectory().toString()
                        + "/abc/"
                        + fpath.toString()}, null,
                newMediaScannerConnection.OnScanCompletedListener() {
                    publicvoidonScanCompleted(String path, Uri uri) {
                        if (uri != null) {
                            context.getContentResolver().delete(uri, null,
                                    null);
                        }
                        fi.delete();
                    }
                });
    } catch (Exception e) {
        e.printStackTrace();
    }

}

It's Good work for me, Hope this help you...if you need any help you can ask

Post a Comment for "Refresh Mediastore For Images & Videos"