【问题标题】:Refresh MediaStore for Images & Videos为图像和视频刷新 MediaStore
【发布时间】:2018-01-22 04:58:33
【问题描述】:

我根据我的要求移动和删除大量图像和视频,现在我正在使用扫描媒体

Intent scanFileIntent = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
sendBroadcast(scanFileIntent);

一切正常,但有时会冻结屏幕,我认为 MediaScanner 存在问题。

我的第二个问题是如何扫描所有媒体存储而不是扫描 perticuler 文件。

提前致谢!!

【问题讨论】:

    标签: android android-studio android-gallery mediastore android-mediascanner


    【解决方案1】:

    请改用MediaScannerConnection

    public void callScanItent(Context context,String path) {
        MediaScannerConnection.scanFile(context,
                new String[] { path }, null,null);
    }
    

     public void callScanItent(Context context,String path) {
        MediaScannerConnection.scanFile(context,
                new String[]{path}, null, new MediaScannerConnection.OnScanCompletedListener() {
                    @Override
                    public void onScanCompleted(String path, Uri uri) {
                        Log.d("Scan complete for: ",path);
                    }
                });
    }
    

    【讨论】:

    • 感谢您的回答,我正在循环删除多个图像,所以我应该每次循环扫描媒体吗?我们不能在循环后扫描吗?
    • 如您所见,第二个参数是要扫描的路径数组。所以是的,你可以在循环结束时传递完整的数组来扫描。
    • 是的,我会试试这个谢谢,给我的问题投票,这样我就可以投票给你,我需要 15 个代表来投票给你。
    • 嗯,它应该像文档所说的那样工作。而且我也测试了它在我的最终工作。检查您传递的路径数组是否应包含有效路径。
    【解决方案2】:

    您应该尝试此解决方案来移动并删除图像。

    在 deleteRecursive(fileOrDirectory) 中传递文件目录路径,您可以从文件或目录中删除多个和单个图像。

    public void deleteRecursive(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);
            }
        }
    
    
    }
    
    private void DeleteAndScanFile(final Context context, String path,
                                   final File fi) {
        String fpath = path.substring(path.lastIndexOf("/") + 1);
        try {
            MediaScannerConnection.scanFile(context, new String[]{Environment
                            .getExternalStorageDirectory().toString()
                            + "/abc/"
                            + fpath.toString()}, null,
                    new MediaScannerConnection.OnScanCompletedListener() {
                        public void onScanCompleted(String path, Uri uri) {
                            if (uri != null) {
                                context.getContentResolver().delete(uri, null,
                                        null);
                            }
                            fi.delete();
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
    
    }
    

    这对我来说很好,希望这对你有帮助......如果你需要任何帮助,你可以问

    【讨论】:

    • 感谢您的回答,我正在循环删除多个图像,所以我应该每次循环扫描媒体吗?我们不能在循环后扫描吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-13
    • 1970-01-01
    • 2020-01-07
    • 1970-01-01
    • 2018-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多