如何在Android Q中打开默认的相册应用并进入特定的相册或文件夹?

huangapple go评论59阅读模式
英文:

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 &lt; 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), &quot;*/*&quot;)
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        startActivity(context, Intent.createChooser(intent, &quot;Open folder&quot;), 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(&quot;image/*&quot;);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,&quot;SelectPicture&quot;),PICK_IMAGE_REQUEST); 

huangapple
  • 本文由 发表于 2020年1月3日 14:36:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574183.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定