简体   繁体   English

更新ContentResolver以获取更新的文件数

[英]Updating ContentResolver to get updated file count

I am deleting few files using Storage Access Framework from External Storage in Marshmallow version of Android. 我正在使用棉花糖版本的Android中的外部存储使用Storage Access Framework删除一些文件。 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. 每当我打开内置文件管理器然后返回到应用程序时,它都会正确更新文件计数,这意味着我需要调用某种方法来更新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); 我试图使用不同的方法来获取正确的文件数,包括调用cursor.setNotificationUri(cr,uri); , notifyChange() on ContentResolver and also using MediaScanner too. ContentResolver上的notifyChange() ,也可以使用MediaScanner 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 . 这里的pathDocumentFile的String Uri,它刚刚使用Storage Access Framework删除了。 I have tried using this also in the notifyChange() method. 我也尝试在notifyChange()方法中使用它。

So how can I update the ContentResolver to return me updated File Count? 那么,如何更新ContentResolver以返回更新后的文件计数?

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) (以上解决方案自android 4.4起似乎不起作用)

for DATA : 对于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/ 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. 您应该在单独的线程上异步地对ContentResolver执行这些操作。 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. 尽管在MediaStore上进行删除根本不需要花费大量时间,但是如果在主线程上执行操作,则会阻塞UI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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