我已经仔细地复制了以前的帖子中的以下代码片段,它可以在模拟器上和我的Nexus 9设备上运行,直到某一点!
但是,我得到的只是一个空的Recent文件夹,我从未到达写入文件的代码.
为了获得合适的文档树,我必须改变什么?
private void testDocumentTree() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, 42);
}
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
String TAG = "onActivityResult";
if (resultCode == RESULT_OK) {
Uri treeUri = resultData.getData();
DocumentFile pickedDir = DocumentFile.fromTreeUri(this, treeUri);
// List all existing files inside picked directory
for (DocumentFile file : pickedDir.listFiles()) {
Log.d(TAG, "Found file " + file.getName() + " with size " + file.length());
}
// Create a new file and write into it
DocumentFile newFile = pickedDir.createFile("text/plain", "My Novel");
try {
OutputStream out = getContentResolver().openOutputStream(newFile.getUri());
out.write("A long time ago...".getBytes());
out.close();
} catch (FileNotFoundException e) {
Log.d(TAG, "File Not Found, reason: ", e);
} catch (IOException e) {
Log.d(TAG,"IOException, reason: ", e);
}
}
}
Run Code Online (Sandbox Code Playgroud)
其他人提到了DocumentsUI上的选项,用户应手动启用该选项。但是,还有另一种选择。将这些额外内容添加到您的ACTION_OPEN_DOCUMENT,ACTION_GET_CONTENT,ACTION_CREATE_DOCUMENT或ACTION_OPEN_DOCUMENT_TREE意向中。打开DocumentsUI应用程序时,“存储”设置上的“浏览”按钮会使用这些额外功能。我认为第一个足以显示内部存储和sdcard。其他人很好。
intent.putExtra("android.content.extra.SHOW_ADVANCED", true);
intent.putExtra("android.content.extra.FANCY", true);
intent.putExtra("android.content.extra.SHOW_FILESIZE", true);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5219 次 |
最近记录: |