次のコードスニペットを以前の投稿から慎重にコピーして、シミュレータ上でも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);
}
}
}
FWIW、私はここでの問題を追加しました:https://code.google.com/p/android/issues/detail?id=202116 –