在Intent返回的目录中创建新文件.ACTION_OPEN_DOCUMENT_TREE [英] Create new file in the directory returned by Intent.ACTION_OPEN_DOCUMENT_TREE
问题描述
在我的App中,用户可以使用隐式意图ACTION_OPEN_DOCUMENT_TREE选择要在其中创建Excel文件的目录.但是, FileOutputStream()
不能使用 onActivityResult()
中返回的Uri.它抛出 FileNotFoundException
:
In my App the user can choose a directory where to create an Excel file using the implicit intent ACTION_OPEN_DOCUMENT_TREE.
However, the Uri returned in onActivityResult()
cannot be used by FileOutputStream()
. It throws a FileNotFoundException
:
java.io.FileNotFoundException: content:/com.android.externalstorage.documents/tree/home%3A:test.xlsx (No such file or directory)
在 onActivityResult()
中,我通过 File.exists()
检查路径是否存在,如果不存在,我想创建一个新的Excel文件.
In onActivityResult()
I check if the path exists via File.exists()
and if not, I want to create a new Excel file.
onActivityResult():
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
Log.d(TAG, "onActivityResult: called");
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && requestCode == 2) {
Log.d(TAG, "onActivityResult: path = " + data.getData()
.getPath());
Uri treePath = data.getData();
File path = new File(treePath + File.pathSeparator + "test.xlsx");
if (path.exists()) {
updateExistingExcelFile(path);
} else {
createNewExcelFile(path);
}
}
}
createNewExcelFile():
private void createNewExcelFile(File path) {
Log.d(TAG, "createNewExcelFile: called");
Workbook workbook = new HSSFWorkbook();
Cell cell;
Sheet sheet;
sheet = workbook.createSheet("Name of sheet");
Row row = sheet.createRow(0);
cell = row.createCell(0);
cell.setCellValue("Name");
cell = row.createCell(1);
cell.setCellValue("Number");
sheet.setColumnWidth(0, (10 * 200));
sheet.setColumnWidth(1, (10 * 200));
FileOutputStream fileOutputStream;
try {
fileOutputStream = new FileOutputStream(path);
workbook.write(fileOutputStream);
Toast.makeText(this, "Created", Toast.LENGTH_LONG)
.show();
fileOutputStream.close();
} catch (IOException e) {
Log.e(TAG, "createNewExcelFile: ", e);
}
}
如果我使用 Activity.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS)
或类似的东西(而不是隐式的意图路径),则代码可以正常工作.
The code works perfectly fine if I use Activity.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS)
or something similar instead of the implicit intents path.
推荐答案
步骤1:以您从 ACTION_OPEN_DOCUMENT_TREE
和
Step #1: Take the Uri
that you get from ACTION_OPEN_DOCUMENT_TREE
and pass it to DocumentFile.fromTreeUri()
.
步骤2:调用在该 DocumentFile
上的 createFile()
以获得代表子文档的 DocumentFile
.
Step #2: Call createFile()
on that DocumentFile
to get a DocumentFile
representing the child document.
步骤3:调用 DocumentFile
上> getUri().
步骤4:调用 ContentResolver
上的 openOutputStream()
,从步骤#3传入 Uri
,以获得 OutputStream
,可用于编写内容.通过在某些 Context
(例如 Activity
)上调用 getContentResolver()
,可以得到 ContentResolver
.
Step #4: Call openOutputStream()
on a ContentResolver
, passing in the Uri
from Step #3, to get an OutputStream
that you can use to write content. You get a ContentResolver
by calling getContentResolver()
on some Context
, such as an Activity
.
请参见此博客文章有关使用 ACTION_OPEN_DOCUMENT_TREE
的更多信息.
See this blog post for more on using ACTION_OPEN_DOCUMENT_TREE
.
这篇关于在Intent返回的目录中创建新文件.ACTION_OPEN_DOCUMENT_TREE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!