Android, Java, 使用存储访问框架(SAF)在SD卡上以编程方式创建目录

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

Android, Java, Create directory on SD card programmatically using Storage Access Framework (SAF)

问题

我的问题是,我无法在插在Android便携设备中的SD卡上创建一个目录。
以下是我正在尝试使其工作的Java代码:我试图在/storage/BF4F-1107/下创建目录sable

public class AnActivity extends AppCompatActivity {
    private static final int N_CREATE_DIRECTORY = 1;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // SD卡
        String s_sdCardStorage = "/storage/BF4F-1107/";

        Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("file/*");
        intent.putExtra(Intent.EXTRA_TITLE, s_destFilePath);
        startActivityForResult(intent, N_CREATE_DIRECTORY);
        // 在这里
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == N_CREATE_DIRECTORY) {
            if(data != null) {
                Uri uri = data.getData();
                DocumentFile docFileSDCardStorage = DocumentFile.fromSingleUri(this, uri);

                try {
                    DocumentFile docFileDir = docFileSDCardStorage.createDirectory("sable");
                } catch(UnsupportedOperationException exn) {
                    System.out.println(exn.getMessage());
                }
            }
        }
    }
}

发生的情况是:

  • onCreate()中的代码被执行。
  • 然后程序在// 在这里处阻塞。
  • 在便携设备上,出现一个显示/storage/BF4F-1107/和一个名为Save的按钮的“对话框”,我“按下”按钮。
  • 一旦“按下”按钮,将执行onActivityResult()中的代码。

但结果是目录/storage/BF4F-1107/下的sable未被创建。
并且执行路径经过catch子句,引发了UnsupportedOperationException异常,System.out.println(exn.getMessage());打印了null

编辑2:在/storage/BF4F-1107/下创建了一个名为_storage_BF4F-1107_的空文件。

您可以帮助我使这段代码工作吗?

此外,我希望可以静默创建目录sable,而不需要用户触摸“保存”。

我正在尝试使用Storage Access Framework(https://developer.android.com/training/data-storage/shared/documents-files),因为java.io.File类的mkdirs方法在我尝试在SD卡上创建目录时不起作用(我收到权限被拒绝的异常)。

编辑:我的Android版本是6.0.1

谢谢。

英文:

My problem is that I do not manage to create a directory on a SD card that is plugged in an Android portable device.
Below is the Java code I am trying to get to work: I am trying to create the directory sable under /storage/BF4F-1107/:

public class AnActivity extends AppCompatActivity
{
private static final int N_CREATE_DIRECTORY = 1;

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        // SD card
        String s_sdCardStorage = "/storage/BF4F-1107/";

        Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("file/*");
        intent.putExtra(Intent.EXTRA_TITLE, s_destFilePath);
        startActivityForResult(intent, N_CREATE_DIRECTORY);
        // HERE
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if(requestCode == N_CREATE_DIRECTORY)
        {
            if(data != null)
            {
                Uri uri = data.getData();
                DocumentFile docFileSDCardStorage = DocumentFile.fromSingleUri(this, uri);

                try
                {
                    DocumentFile docFileDir = docFileSDCardStorage.createDirectory("sable");
                }
                catch(UnsupportedOperationException exn)
                {
                    System.out.println(exn.getMessage());
                }
            }
        }
    }
}

What happens is:

  • the code in onCreate() is executed.
  • Then the program blocks at // HERE.
  • On the portable device, appears a "dialog" which shows /storage/BF4F-1107/ and a button Save which I "press".
  • Once "pressed", the code in onActivityResult() is executed.

But the result is that the directory sable under /storage/BF4F-1107/ is not created.
And the execution path goes through the catch clause, the exception UnsupportedOperationException is raised and null is printed at System.out.println(exn.getMessage());.

Edit 2: An empty file _storage_BF4F-1107_ is created under /storage/BF4F-1107/.

Can you help me make this code work?

Additionally, I would like the directory sable to be created silently.
I do not want to user to have to touch "Save".

I am trying to use the Storage Access Framework (https://developer.android.com/training/data-storage/shared/documents-files) because the mkdirs method of the java.io.File class doesn't work (I get permission denied exceptions) when I try to create a directory on the SD card.

Edit: my Android version is 6.0.1

Thank you.

答案1

得分: 1

使用ACTION_OPEN_DOCUMENT_TREE让用户选择SD卡。

之后,您可以在选择的目录中创建许多文件和目录。

如果您只想使用SAF创建一个文件,请使用ACTION_CREATE_DOCUMENT,用户可以选择位置和文件名。

英文:

Use ACTION_OPEN_DOCUMENT_TREE to let the user choose de SD card.

After that you can create as many files and directorys in the choosen directory.

If you only want to create one file with SAF use ACTION_CREATE_DOCUMENT where the user chooses the location and file name.

huangapple
  • 本文由 发表于 2020年8月5日 18:24:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63263149.html
匿名

发表评论

匿名网友

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

确定