我尝试打开图库中的特定文件夹,如下代码所示,但它对我不起作用,并且出现错误无法找到物品.
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())
}
操作系统中从未支持过这一点。这是设备上安装的应用程序的问题。
从来没有要求设备有一个能够响应的图库应用程序ACTION_VIEW
对于一个目录。
你的应用程序应该会崩溃FileUriExposedException
在 Android 7.0+ 上,该异常的存在就是为什么越来越少的应用程序愿意支持file
scheme.
在 Android 10+ 上,file
计划(和Uri.fromFile()
通过扩展)是毫无意义的,就好像您的应用程序可以访问该文件,其他应用程序不能。
最后,Environment.DIRECTORY_PICTURES.plus(File.separator).plus(directory)
不是有效的文件系统路径。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)