Android - 从内部存储文件夹中选择文件

时间:2016-05-04 16:47:37

标签: android

我在内部存储文件夹中保存了一堆视频。之后,我希望用户能够在此特定文件夹中选择其中一个视频。我尝试使用ACTION_GET_CONTENT尝试让另一个应用程序为我这样做,但没有任何成功,因为它只是在其他目录中打开了一个文件浏览器。

我现在拥有的是:

public static File getOwnVideosDirectory(Context context) {
    String ownVideosDirPath =
            context.getFilesDir().getAbsolutePath() + File.separator + "OwnVideos";
    File ownVideosDir = new File(ownVideosDirPath);
    if (!ownVideosDir.exists()) {
        ownVideosDir.mkdirs();
    }
    return ownVideosDir;
}

private void dispatchExistingVideo() {
    Log.d(LOG_TAG, "> dispatchExistingVideo");
    Intent videoPicker = new Intent(Intent.ACTION_GET_CONTENT);
    File ownVideosDir = Utility.getOwnVideosDirectory(getContext());
    videoPicker.setDataAndType(Uri.fromFile(ownVideosDir), "video/*");
    if (videoPicker.resolveActivity(getContext().getPackageManager()) != null) {
        startActivityForResult(videoPicker, REQUEST_EXISTING_VIDEO);
    }
}

所以我想知道,我做错了什么,或者这样做是不可能的。如果不可能:是否有任何图书馆,...可以让我做我想做的事情,或者我自己作为最后手段如何实现这个目标的方向?

提前致谢

1 个答案:

答案 0 :(得分:3)

请查看该图书馆 - Material File Picker
它允许使用.withPath(Utility.getOwnVideosDirectory(getContext()).getAbsolutePath())显示包含指定路径的对话框。

整个创作代码:

new MaterialFilePicker()
    .withActivity(this)
    .withRequestCode(1)
    .withFilter(Pattern.compile(".*\\.txt$")) // Filtering files and directories by file name using regexp
    .withFilterDirectories(true) // Set directories filterable (false by default)
    .withHiddenFiles(true) // Show hidden files and folders
    .withPath(Utility.getOwnVideosDirectory(getContext()).getAbsolutePath())
    .start();