媒体扫描特定文件夹

Media scanning specific folder

我正在寻找可以扫描特定文件夹中文件的媒体扫描源。
或扫描特定文件夹中特定扩展文件(如 .mp4)的来源。
谢谢。

使用以下代码从特定文件夹获取文件:

File folder_file = new File("give specific folder path");
    File[] files = folder_file.listFiles();
    if (files != null) {
        for (File file : files) {
            // checking the File is file or directory
            if (file.isFile()) {
                String path = file.getAbsolutePath();
                String extension = path
                        .substring(path.lastIndexOf(".") + 1);
                // if the file is audio type, then save it to the database
                if (extension.equalsIgnoreCase("mp4")) {
                    System.out.println(path + " is a media file ");
                }
            }
        }
    }

试试这个扫描文件:

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

I'm finding a media scanning source that can find files in specific folder

MediaScannerConnection.scanFile 将第二个参数作为待扫描路径的字符串数组

source that scans specific extension files (Like .mp4) in specific folder.

MediaScannerConnection.scanFile 将第三个参数作为我们要扫描的 mimeTypes 的字符串数组。

示例:

String[] paths = {path_to_scan};
String[] mimeTypes = {"video/mp4"};
MediaScannerConnection.scanFile(getApplicationContext(),
                                paths,
                                mimeTypes,
                              new MediaScannerConnection.OnScanCompletedListener() {
                               public void onScanCompleted(String path, Uri uri) {
                                 // scanned path and uri
                              }
                         });