如何使用Storage Access Framework在SD卡中创建新文件夹? [英] How to create new folder in sd card by using Storage Access Framework?

查看:293
本文介绍了如何使用Storage Access Framework在SD卡中创建新文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用存储访问框架在SD卡上创建新文件夹。如果您给我代码,那将是非常好的。
我已经搜索了其他问题和答案,但没有找到方法。

I would like to know how to use "Storage Access Framework" to create new folder on SD card. If you give me the code it would be very good. I have already searched other questions and answers but not found how to.

为 CommonsWare答案添加一些已经有效的代码。
请注意,这是我发现它可以在装有Android OS 5.1.1的手机SS A5上的SD卡中创建新文件夹的唯一方法

Add some codes that already work per "CommonsWare" answer. Note that is the only way I found that it can make new folder in sd card on my phone SS A5 with Android OS 5.1.1

    public void newFolder(View view)
    {
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
        startActivityForResult(intent, NEW_FOLDER_REQUEST_CODE);
    }

    private static final int NEW_FOLDER_REQUEST_CODE = 43;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent resultData) {
        super.onActivityResult(requestCode, resultCode, resultData);

        Uri currentUri = null;

        if (resultCode == Activity.RESULT_OK)
        {
            if (requestCode == NEW_FOLDER_REQUEST_CODE)
            {
                if (resultData != null) {
                    currentUri = resultData.getData();
                    DocumentFile pickedDir = DocumentFile.fromTreeUri(this, currentUri);
                    DocumentFile newDir = pickedDir.createDirectory("MyFolder");
                    textView.setText(newDir.getName());
                }
            }
        }
    }


推荐答案

您不能创建在SD卡上创建新文件夹。您可以在用户选择的其他文件夹中创建一个新文件夹,但是不能强制用户选择可移动存储。

You cannot create "create new folder on SD card". You can create a new folder inside some other folder that the user chooses, but you cannot force the user to choose removable storage.

要在其他文件夹中创建一个新文件夹,应工作:

To create a new folder inside of some other folder, this should work:


  1. startActivityForResult()开始活动://developer.android.com/reference/android/content/Intent.html#ACTION_OPEN_DOCUMENT_TREE rel = nofollow noreferrer> ACTION_OPEN_DOCUMENT_TREE 意图 ,以允许用户选择文件夹。 包括 FLAG_DIR_SUPPORTS_CREATE 以确保您可以在文件夹中创建新内容。

  1. Start an activity with startActivityForResult() on an ACTION_OPEN_DOCUMENT_TREE Intent, to allow the user to choose a folder. Include FLAG_DIR_SUPPORTS_CREATE to ensure that you can create something new in the folder.

onActivityResult()中,将在 Uri 包裹起来> DocumentFile ,然后在其上调用 createDirectory() 来创建一个新文件夹,作为用户选择的子文件夹。

In onActivityResult(), wrap the Uri that you get in a DocumentFile, then call createDirectory() on it to create a new folder as a child of whatever the user chose.

这篇关于如何使用Storage Access Framework在SD卡中创建新文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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