简体   繁体   中英

Updating ContentResolver to get updated file count

I am deleting few files using Storage Access Framework from External Storage in Marshmallow version of Android. I need to show the count of different kinds of files available in the External Storage. I am getting proper count for Internal Storage, but for External Storage I am not getting updated file count for Videos. Whenever I open an inbuilt File Manager and then get back to the App, it updates the File count properly which means I need to call some kind of method to update the ContentResolver.

Here's how I am getting the count of Videos in External Storage

private long[] findSize(StorageSource source, FileTypes type) {
        Uri uri = MediaStore.Files.getContentUri(CONTENT_URI_COMMON);
        String[] projection = { MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.SIZE };
        String selection = getSelectionQuery(type);
        String[] selectionArgs = {};
        long total = 0;
        int count = 0;
        String sortOrder = null;
        Cursor allExtMediaFiles = populateQueries(uri, projection, selection, selectionArgs, sortOrder);
        synchronized (allExtMediaFiles) {
            if (allExtMediaFiles != null) {
                if (allExtMediaFiles.moveToFirst())

                    do {
                        if (isValidFamily(allExtMediaFiles.getString(0), source)) {
                            total = total + allExtMediaFiles.getLong(1);
                            count++;
                        }
                    } while (allExtMediaFiles.moveToNext());
                allExtMediaFiles.close();
            }
        }
        return new long[] { total, count };
    }

private Cursor populateQueries(Uri uri, String[] projection, String selection, String[] selectionArgs,
            String sortOrder) {
        ContentResolver cr = context.getContentResolver();
        Cursor cursor = cr.query(uri, projection, selection, selectionArgs, sortOrder);
        cursor.setNotificationUri(cr,uri);
        return cursor;
    }

I have tried to get correct file count using different methods including calling cursor.setNotificationUri(cr,uri); , notifyChange() on ContentResolver and also using MediaScanner too. Here's the code I am using after I have deleted the video file -

Uri uri = MediaStore.Files.getContentUri("external");
                    getActivity().getContentResolver().notifyChange(uri, null);
                    MediaScannerConnection.scanFile(getActivity(), new String[] {
                                    path},
                            null, new MediaScannerConnection.OnScanCompletedListener() {
                                public void onScanCompleted(String scanPath, Uri scanUri)
                                {
                                    Log.d(TAG,"Path "+scanPath+" uri"+scanUri.toString());
                                }
                            });

Here path is the String Uri from DocumentFile which just got deleted using Storage Access Framework . I have tried using this also in the notifyChange() method.

So how can I update the ContentResolver to return me updated File Count?

try this line after delete function

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

(above solution doesn't seem to work since android 4.4)

for DATA :

MediaStore.MediaColumns.DATA

also you can try using this

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(fileDeleted)));

see this link for a (kind of) detailed method:

http://sandersdenardi.com/querying-and-removing-media-from-android-mediastore/

also keep in mind that:

All queries and deletes (and inserts and updates for that matter) block until the underlying transaction completes. You should perform these operations on the ContentResolver asynchronously on a separate thread. While a delete on the MediaStore shouldn't take a significant amount of time at all, it will block the UI if performed on the main thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM