intent.setDataAndType() 不适用于 android 5
intent.setDataAndType() not working on android 5
我正在尝试按以下方式使用 Intent 从特定文件夹中选择图像:
private void selectPicture(){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath() + "/Pictures/");
intent.setDataAndType(uri, "image/*");
startActivityForResult(intent, 1);
}
图像选择器已正确启动。但是,我看到了所有图像(包括来自我指定目录的图像和来自 phone 上其他位置的图像),而不是仅来自指定目录的图像。
如果有任何关于如何让它发挥作用的提示,我将不胜感激。
我将应用程序的图像移动到 Pictures 目录只是因为我无法在应用程序的私有 externalFilesDir() 中显示这些图像。如果您知道如何让此功能适用于应用程序外部文件目录中的图像,我也将非常感激。
谢谢!
ACTION_GET_CONTENT
从未设计为使用 Uri
,当然也没有记录只允许用户选择 "those from the specified directory only"。引用 the documentation for ACTION_GET_CONTENT
:
Note that no URI is supplied in the intent, as there are no constraints on where the returned data originally comes from.
欢迎您创建自己的内容浏览 UI,将用户限制在特定目录,也许使用 MediaStore
查找相关媒体以供用户选择。
此外,在 Android 4.4+ 上,您可以使用 the Storage Access Framework 允许用户选择内容。可能有一个流程允许您以编程方式 "pick" 目录,然后将其用作系统的基础以允许用户选择该目录中的内容。但是,我还没有看到这样做——通常,用户会从所有可用存储提供程序的根目录开始。
我正在尝试按以下方式使用 Intent 从特定文件夹中选择图像:
private void selectPicture(){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath() + "/Pictures/");
intent.setDataAndType(uri, "image/*");
startActivityForResult(intent, 1);
}
图像选择器已正确启动。但是,我看到了所有图像(包括来自我指定目录的图像和来自 phone 上其他位置的图像),而不是仅来自指定目录的图像。
如果有任何关于如何让它发挥作用的提示,我将不胜感激。
我将应用程序的图像移动到 Pictures 目录只是因为我无法在应用程序的私有 externalFilesDir() 中显示这些图像。如果您知道如何让此功能适用于应用程序外部文件目录中的图像,我也将非常感激。
谢谢!
ACTION_GET_CONTENT
从未设计为使用 Uri
,当然也没有记录只允许用户选择 "those from the specified directory only"。引用 the documentation for ACTION_GET_CONTENT
:
Note that no URI is supplied in the intent, as there are no constraints on where the returned data originally comes from.
欢迎您创建自己的内容浏览 UI,将用户限制在特定目录,也许使用 MediaStore
查找相关媒体以供用户选择。
此外,在 Android 4.4+ 上,您可以使用 the Storage Access Framework 允许用户选择内容。可能有一个流程允许您以编程方式 "pick" 目录,然后将其用作系统的基础以允许用户选择该目录中的内容。但是,我还没有看到这样做——通常,用户会从所有可用存储提供程序的根目录开始。