简体   繁体   English

如何从 android 中的意图打开目录?

[英]How to open a directory from intent in android?

I am trying to open a directory with the help of an intent to show the user what is the content inside that folder but I am unable to do so, but I don't know why the folder won't open and I get this "Can't use this folder" on the file manager.我试图借助向用户显示该文件夹内的内容的意图打开一个目录,但我无法这样做,但我不知道为什么该文件夹不会打开,我得到了这个“无法使用此文件夹”在文件管理器中。

在此处输入图像描述

               open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + "/" + "XYZ";
                Uri uri = Uri.parse(path);
                Toast.makeText(MainActivity.this, ""+path, Toast.LENGTH_SHORT).show();

                openDirectory(uri);

            }
        });

The method方法

public void openDirectory(Uri uriToLoad){
        // Choose a directory using the system's file picker.
        int result = 1;

        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);

        // Optionally, specify a URI for the directory that should be opened in
        // the system file picker when it loads.
        intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, uriToLoad);

        startActivityForResult(intent, result);
    }

I want to open the folder XYZ after clicking on the button我想在点击按钮后打开文件夹 XYZ

I am trying to open a directory with the help of an intent to show the user what is the content inside that folder我正在尝试借助向用户显示该文件夹内的内容的意图打开一个目录

There is no standard Intent action for that, sorry.抱歉,没有标准的Intent操作。 Your code is trying to let the user select a document tree.您的代码试图让用户 select 成为文档树。

It is also doing that incorrectly, as EXTRA_INITIAL_URI does not take a file:// Uri as a value.它也这样做不正确,因为EXTRA_INITIAL_URI没有将file:// Uri作为值。 That needs to be some Uri that you obtained previously from the Storage Access Framework, such as via some past ACTION_OPEN_DOCUMENT_TREE request.这需要是您之前从存储访问框架获得的一些Uri ,例如通过一些过去的ACTION_OPEN_DOCUMENT_TREE请求。

I don't know why the folder won't open and I get this "Can't use this folder" on the file manager我不知道为什么文件夹打不开,我在文件管理器上看到“无法使用这个文件夹”

From Android's standpoint, your EXTRA_INITIAL_URI value is little better than a random string.从 Android 的角度来看,您的EXTRA_INITIAL_URI值比随机字符串好一点。

But the user won't know where actually XYZ folder is但用户不会知道 XYZ 文件夹的实际位置

Then perhaps you should have let the user choose the location in the first place, rather than forcing a particular location.那么也许你应该让用户首先选择位置,而不是强制一个特定的位置。 For example, you could use ACTION_CREATE_DOCUMENT to let the user decide where to place the document on the user's device (or the user's cloud storage, the user's network file server, etc.).例如,您可以使用ACTION_CREATE_DOCUMENT让用户决定将文档放在用户设备上的哪个位置(或用户的云存储、用户的网络文件服务器等)。

As far as I know using Intent you can browse, open and create files, that shared for all apps by the system.据我所知,使用 Intent 您可以浏览、打开和创建文件,这些文件由系统为所有应用程序共享。 In this case for get file path to open, you can use next code like this (pardon for my Kotlin):在这种情况下,要打开获取文件路径,您可以使用如下代码(请原谅我的 Kotlin):

private var launcherForResult =
    registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
        if (result.resultCode == Activity.RESULT_OK) {
            result.data.also { uri -> filePath = uri.toString() }
        }
    }

private fun getFilePath() {
    val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
        type = "*/*"
        addCategory(Intent.CATEGORY_OPENABLE)
    }
    launcherForResult.launch(intent)
}

As for the directory, it usually opens the last one that was opened before.至于目录,它通常会打开之前打开的最后一个目录。

If you don't want other applications to see your files, store them in your application's private directory如果您不希望其他应用程序看到您的文件,请将它们存储在应用程序的私有目录中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM