使用存储访问框架打开特定目录

Hai*_*eon 9 android android-intent storage-access-framework android-5.0-lollipop

我现在可以通过使用以下代码打开Lollipop文件选择器来获取文件URI:

            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_OPEN_DOCUMENT_TREE);
            startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

文件选择器在内部存储的根目录或我打开的最后一个目录中打开.如何让文件选择器显示特定目录的内容?例如,我可能希望文件选择器在启动后显示Download文件夹的内容,如下图所示.

打开的子目录

Ben*_*ila -2

使用具有类似方法的EnvironmentgetExternalStorageDirectory()类,或者getDataDirectory() https://developer.android.com/reference/android/os/Environment.html

如果图像存储在变量中,请从该路径获取它们:

private static final String fileName = Environment
            .getExternalStorageDirectory().getAbsolutePath()
            + "/pathToFile.jpg";
Run Code Online (Sandbox Code Playgroud)

  • `getExternalStorageDirectory()` 已被弃用。 (2认同)