使用Intent打开特定的文件夹 [英] Open specific folder using Intent

查看:145
本文介绍了使用Intent打开特定的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Intent打开特定路径的文件夹,我使用了文件资源管理器的代码,它可以正常工作,但是在某些设备(三星设备)中,如果文件资源管理器应用程序不可用,则无法打开特定路径的文件夹.我尝试了许多解决方案,但对我而言不起作用.

I want to open specific path folder using Intent, I used code for File explorer and It working perfectly but in some device (samsung devices) if file explorer app are not available then It not opening folder of specific path. I tried many solutions but It won't work for me.

Uri uri = Uri.fromFile(new File(new File(filePath).getParent()));
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "resource/folder");


        if (intent.resolveActivityInfo(getPackageManager(), 0) != null)
        {
            startActivity(intent);
        }
        else {

           Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
           intent.setDataAndType(uri, "file/*");
           startActivity(Intent.createChooser(intent, "Open folder"));

        }

推荐答案

这是我访问"root/Downloads/Sam"的解决方案.在科特林

Here is my solution for accessing "root/Downloads/Sam" in Kotlin

val rootPath = "content://com.android.externalstorage.documents/document/primary:"
val samPath = Uri.parse("$rootPath${Environment.DIRECTORY_DOWNLOADS}%2fSam")    //"%2f" represents "/"

val REQUEST_CODE_PICK_FILE = 1

main_sam_button.setOnClickListener {
    val intent = Intent(Intent.ACTION_GET_CONTENT)                                  

    intent.type = "text/plain"    //specify file type
    intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, samPath)    //set the default folder

    startActivityForResult(intent, REQUEST_CODE_PICK_FILE)
}

演示: https://youtu.be/lUIPyC_8q_M

有用的阅读内容:

这篇关于使用Intent打开特定的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆