使用Intent打开特定文件夹

时间:2017-07-27 05:46:08

标签: android android-intent

我想使用Intent打开特定的路径文件夹,我使用File explorer的代码,它在一些设备(三星设备)中工作得很好,如果文件浏览器应用程序不可用,那么它不会打开特定路径的文件夹。 我尝试了很多解决方案,但这对我不起作用。

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"));

        }

2 个答案:

答案 0 :(得分:1)

        val intent = Intent(Intent.ACTION_GET_CONTENT)
        intent.setDataAndType(
            Uri.parse(
                (Environment.getExternalStorageDirectory().path
                        + java.io.File.separator) + "myFile" + java.io.File.separator
            ), "*/*"
        )
        intent.addFlags(
            Intent.FLAG_GRANT_READ_URI_PERMISSION
                    and Intent.FLAG_GRANT_WRITE_URI_PERMISSION
                    and Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
                    and Intent.FLAG_GRANT_PREFIX_URI_PERMISSION
        )
        intent.addCategory(Intent.CATEGORY_OPENABLE)
        startActivityForResult(intent, WRITE_ACCESS_CODE)

答案 1 :(得分:0)

这是我在 Kotlin

中访问“ root / Downloads / Sam”的解决方案
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)
}