README.md
1Android DirectorySelection Sample
2===================================
3
4A basic app showing how to use Directory Selection API to let users
5select an entire directory subtree, which extends the Storage Access Framework
6introduced in Android 4.4 (API level 19).
7
8Introduction
9------------
10
11The [Directory Selection][1] API, which was introduced in Android 5.0 (API level 21)
12extends the [Storage Access Framework][2] to let users select an entire directory subtree,
13giving apps read/write access to all contained documents without requiring user
14confirmation for each item.
15
16To select a directory subtree, build and send an [OPEN_DOCUMENT_TREE intent][3] like in the
17following code:
18
19```java
20Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
21startActivityForResult(intent, REQUEST_CODE_OPEN_DIRECTORY);
22```
23
24The system displays all [DocumentsProvider][4] instances that support subtree selection,
25 letting the user browse and select a directory.
26
27The returned URI represents access to the selected subtree. You can then use
28[buildChildDocumentsUriUsingTree()][5] to access to the child documents and
29[buildDocumentUriUsingTree()][6] to access to the selected directory itself along with [query()][7]
30to explore the subtree.
31
32This example explores the child documents and the selected document by following code:
33
34```java
35@Override
36public void onActivityResult(int requestCode, int resultCode, Intent data) {
37 super.onActivityResult(requestCode, resultCode, data);
38 if (requestCode == REQUEST_CODE_OPEN_DIRECTORY && resultCode == Activity.RESULT_OK) {
39 updateDirectoryEntries(data.getData());
40 }
41}
42
43void updateDirectoryEntries(Uri uri) {
44 ContentResolver contentResolver = getActivity().getContentResolver();
45 Uri docUri = DocumentsContract.buildDocumentUriUsingTree(uri,
46 DocumentsContract.getTreeDocumentId(uri));
47 Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(uri,
48 DocumentsContract.getTreeDocumentId(uri));
49
50 Cursor docCursor = contentResolver.query(docUri, new String[]{
51 Document.COLUMN_DISPLAY_NAME, Document.COLUMN_MIME_TYPE}, null, null, null);
52 try {
53 while (docCursor.moveToNext()) {
54 Log.d(TAG, "found doc =" + docCursor.getString(0) + ", mime=" + docCursor
55 .getString(1));
56 mCurrentDirectoryUri = uri;
57 mCurrentDirectoryTextView.setText(docCursor.getString(0));
58 mCreateDirectoryButton.setEnabled(true);
59 }
60 } finally {
61 closeQuietly(docCursor);
62 }
63
64 Cursor childCursor = contentResolver.query(childrenUri, new String[]{
65 Document.COLUMN_DISPLAY_NAME, Document.COLUMN_MIME_TYPE}, null, null, null);
66 try {
67 List<DirectoryEntry> directoryEntries = new ArrayList<>();
68 while (childCursor.moveToNext()) {
69 Log.d(TAG, "found child=" + childCursor.getString(0) + ", mime=" + childCursor
70 .getString(1));
71 DirectoryEntry entry = new DirectoryEntry();
72 entry.fileName = childCursor.getString(0);
73 entry.mimeType = childCursor.getString(1);
74 directoryEntries.add(entry);
75 }
76 mAdapter.setDirectoryEntries(directoryEntries);
77 mAdapter.notifyDataSetChanged();
78 } finally {
79 closeQuietly(childCursor);
80 }
81}
82```
83
84Also, the new [createDocument()][8] method lets you create new documents or directories
85anywhere under the subtree.
86
87This example creates a new directory by following code:
88
89```java
90ContentResolver contentResolver = getActivity().getContentResolver();
91Uri docUri = DocumentsContract.buildDocumentUriUsingTree(uri,
92 DocumentsContract.getTreeDocumentId(uri));
93Uri directoryUri = DocumentsContract
94 .createDocument(contentResolver, docUri, Document.MIME_TYPE_DIR, directoryName);
95```
96
97[1]: https://developer.android.com/about/versions/android-5.0.html#Storage
98[2]: https://developer.android.com/guide/topics/providers/document-provider.html
99[3]: https://developer.android.com/reference/android/content/Intent.html#ACTION_OPEN_DOCUMENT_TREE
100[4]: https://developer.android.com/reference/android/provider/DocumentsProvider.html
101[5]: https://developer.android.com/reference/android/provider/DocumentsContract.html#buildChildDocumentsUriUsingTree(android.net.Uri%2C%20java.lang.String)
102[6]: https://developer.android.com/reference/android/provider/DocumentsContract.html#buildDocumentUriUsingTree(android.net.Uri%2C%20java.lang.String)
103[7]: https://developer.android.com/reference/android/content/ContentResolver.html#query(android.net.Uri%2C%20java.lang.String%5B%5D%2C%20java.lang.String%2C%20java.lang.String%5B%5D%2C%20java.lang.String)
104[8]: https://developer.android.com/reference/android/provider/DocumentsContract.html#createDocument(android.content.ContentResolver%2C%20android.net.Uri%2C%20java.lang.String%2C%20java.lang.String)
105
106Pre-requisites
107--------------
108
109- Android SDK v21
110- Android Build Tools v21.1.1
111- Android Support Repository
112
113Screenshots
114-------------
115
116<img src="screenshots/screenshot-1.png" height="400" alt="Screenshot"/> <img src="screenshots/screenshot-2.png" height="400" alt="Screenshot"/> <img src="screenshots/screenshot-3.png" height="400" alt="Screenshot"/>
117
118Getting Started
119---------------
120
121This sample uses the Gradle build system. To build this project, use the
122"gradlew build" command or use "Import Project" in Android Studio.
123
124Support
125-------
126
127- Google+ Community: https://plus.google.com/communities/105153134372062985968
128- Stack Overflow: http://stackoverflow.com/questions/tagged/android
129
130If you've found an error in this sample, please file an issue:
131https://github.com/googlesamples/android-DirectorySelection
132
133Patches are encouraged, and may be submitted by forking this project and
134submitting a pull request through GitHub. Please see CONTRIBUTING.md for more details.
135
136License
137-------
138
139Copyright 2014 The Android Open Source Project, Inc.
140
141Licensed to the Apache Software Foundation (ASF) under one or more contributor
142license agreements. See the NOTICE file distributed with this work for
143additional information regarding copyright ownership. The ASF licenses this
144file to you under the Apache License, Version 2.0 (the "License"); you may not
145use this file except in compliance with the License. You may obtain a copy of
146the License at
147
148http://www.apache.org/licenses/LICENSE-2.0
149
150Unless required by applicable law or agreed to in writing, software
151distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
152WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
153License for the specific language governing permissions and limitations under
154the License.
155