英文:
How to open default gallery app with particular album or folder for Android Q?
问题
I have tried to open a particular folder in the gallery using the following code, but it didn't work for me, and I received an error message stating Unable to find item.
fun openDirectoryInGallery(context: Context, directory: String) {
val intent = Intent(Intent.ACTION_VIEW)
val file = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), directory)
} else {
File(Environment.DIRECTORY_PICTURES.plus(File.separator).plus(directory))
}
intent.setDataAndType(Uri.withAppendedPath(Uri.fromFile(file), directory), "*/*")
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
startActivity(context, Intent.createChooser(intent, "Open folder"), Bundle())
}
英文:
I have tried to open particular folder in gallary as below code but it didn't work for me, and getting error for Unable to find item.
fun openDirectoryInGallery(context: Context, directory: String) {
val intent = Intent(Intent.ACTION_VIEW)
val file = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), directory)
} else {
File(Environment.DIRECTORY_PICTURES.plus(File.separator).plus(directory))
}
intent.setDataAndType(Uri.withAppendedPath(Uri.fromFile(file), directory), "*/*")
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
startActivity(context, Intent.createChooser(intent, "Open folder"), Bundle())
}
答案1
得分: 3
在操作系统中从未支持过这个功能。这是设备上安装的应用程序的问题。
从来没有要求设备必须有一个能够响应ACTION_VIEW指令的相册应用程序。
在Android 7.0+上,您的应用程序应该会崩溃,并出现FileUriExposedException异常,这也是越来越少的应用程序不再支持file方案的原因。
在Android 10+上,file方案(以及Uri.fromFile())实际上没有意义,因为如果您的应用程序可以访问文件,其他应用程序无法访问。
最后,Environment.DIRECTORY_PICTURES.plus(File.separator).plus(directory) 不是有效的文件系统路径。
英文:
There has never been support for this in the OS. This is a question of the installed apps on the device.
There has never been a requirement that a device have a gallery app that is able to respond to ACTION_VIEW for a directory.
Your app should be crashing with a FileUriExposedException on Android 7.0+, and the existence of that exception is why fewer and fewer apps are bothering to support the file scheme.
On Android 10+, the file scheme (and Uri.fromFile() by extension) is simply pointless, as if your app can access the file, other apps cannot.
Finally, Environment.DIRECTORY_PICTURES.plus(File.separator).plus(directory) is not a valid filesystem path.
答案2
得分: -4
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "选择图片"), PICK_IMAGE_REQUEST);
英文:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"SelectPicture"),PICK_IMAGE_REQUEST);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论