英文:
Create Folder using storage access framework
问题
我是 Android Studio 的新手。我想通过 Android 应用在外部存储中创建文件夹。我尝试使用 getExternalStorageDir()
,但在 API 等级高于 23 的情况下未实现。请指导我如何在 Android 应用中创建文件夹和文件。如果有语法错误,请忽略。我还希望在后台完成此过程,而无需离开主活动界面。
英文:
I am knew to Android Studio. I want to create folder in external storage through android app. I tried using getExternalStorageDir()
but it is not implemented for API level above 23. Please guide me to create folder and file in android app. Ignore any grammatical mistake if present. And I also want this process to be done in background without going anywhere from mainactivity.
答案1
得分: 0
用户可以在使用 SAF(Storage Access Framework)选择器时选择创建新文件夹,但是无法在外部存储中无需任何用户交互的情况下以编程方式创建文件夹。
例如,在布局中添加一个按钮并为其添加一个 OnClickListener。然后在单击时运行以下代码:
int OPEN_REQUEST_CODE = 41;
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_TITLE, "Notes");
startActivityForResult(intent, OPEN_REQUEST_CODE);
这将提示用户创建一个名为 "Notes" 的文档,但用户可以选择存储文档的位置,甚至可以选择重命名。在选择器中,有一个名为 "New folder" 的选项,用户可以选择它。
Scoped Storage 的部分理念是不希望开发人员在外部存储中随意创建各种文件和文件夹。因此,这使用户更加掌控。
一旦用户选择了文件夹,您可以创建子文件夹,但这仍然需要提示用户授予对所选文件夹的一次性访问权限:
https://stackoverflow.com/a/36547137/7434090
此外,在我学习 SAF 时,这篇教程对我非常有帮助:
https://en.proft.me/2018/05/24/using-android-storage-access-framework/
英文:
The user can choose to make a new folder while using the SAF picker, but you can't programmatically create a folder in external storage without any user interaction.
For instance, add a Button to your layout and give it an OnClickListener. Then have it run this code when clicked:
int OPEN_REQUEST_CODE = 41;
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_TITLE, "Notes");
startActivityForResult(intent, OPEN_REQUEST_CODE);
This will prompt the user to create a document titled "Notes", but the user can choose where the document is stored and can even rename it if they so choose. Within the picker, there's an option named "New folder" that the user can select.
Part of the idea by Scoped Storage is that they don't want us devs to just be polluting the external storage with all sorts of files and folders. So this puts the user more in control.
You can make a subfolder once the user chooses a folder, but this still requires prompting the user to grant one-time access to a chosen folder:
https://stackoverflow.com/a/36547137/7434090
Also, this tutorial was very helpful to me while learning SAF:
https://en.proft.me/2018/05/24/using-android-storage-access-framework/
答案2
得分: 0
放在public void中:
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, NEW_FOLDER_REQUEST_CODE);
放在Activity Result中:
if (_resultCode == Activity.RESULT_OK) {
if (_requestCode == NEW_FOLDER_REQUEST_CODE) {
if (_data != null) {
Uri currentUri = _data.getData();
DocumentFile pickedDir = DocumentFile.fromTreeUri(this, currentUri);
DocumentFile newDir = pickedDir.createDirectory("MyFolder");
}
}
}
放在另一个public void中的onCreate:
private static final int NEW_FOLDER_REQUEST_CODE = 43;
英文:
Put this in public void
Intent intent = new
Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, NEW_FOLDER_REQUEST_CODE);
This on Activity Result
if (_resultCode == Activity.RESULT_OK) { if (_requestCode ==
NEW_FOLDER_REQUEST_CODE)
{ if (_data != null) { Uri currentUri = _data.getData();
DocumentFile pickedDir =
DocumentFile.fromTreeUri(this, currentUri);
DocumentFile newDir =
pickedDir.createDirectory("MyFolder"); } } }
This is another public void on oncreate
}
private static final int NEW_FOLDER_REQUEST_CODE = 43;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论