README.md
1 Android DirectorySelection Sample
2 ===================================
3
4 A basic app showing how to use Directory Selection API to let users
5 select an entire directory subtree, which extends the Storage Access Framework
6 introduced in Android 4.4 (API level 19).
7
8 Introduction
9 ------------
10
11 The [Directory Selection][1] API, which was introduced in Android 5.0 (API level 21)
12 extends the [Storage Access Framework][2] to let users select an entire directory subtree,
13 giving apps read/write access to all contained documents without requiring user
14 confirmation for each item.
15
16 To select a directory subtree, build and send an [OPEN_DOCUMENT_TREE intent][3] like in the
17 following code:
18
19 ```java
20 Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
21 startActivityForResult(intent, REQUEST_CODE_OPEN_DIRECTORY);
22 ```
23
24 The system displays all [DocumentsProvider][4] instances that support subtree selection,
25 letting the user browse and select a directory.
26
27 The 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]
30 to explore the subtree.
31
32 This example explores the child documents and the selected document by following code:
33
34 ```java
35 @Override
36 public 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
43 void 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
84 Also, the new [createDocument()][8] method lets you create new documents or directories
85 anywhere under the subtree.
86
87 This example creates a new directory by following code:
88
89 ```java
90 ContentResolver contentResolver = getActivity().getContentResolver();
91 Uri docUri = DocumentsContract.buildDocumentUriUsingTree(uri,
92 DocumentsContract.getTreeDocumentId(uri));
93 Uri 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
106 Pre-requisites
107 --------------
108
109 - Android SDK v21
110 - Android Build Tools v21.1.1
111 - Android Support Repository
112
113 Screenshots
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
118 Getting Started
119 ---------------
120
121 This sample uses the Gradle build system. To build this project, use the
122 "gradlew build" command or use "Import Project" in Android Studio.
123
124 Support
125 -------
126
127 - Google+ Community: https://plus.google.com/communities/105153134372062985968
128 - Stack Overflow: http://stackoverflow.com/questions/tagged/android
129
130 If you've found an error in this sample, please file an issue:
131 https://github.com/googlesamples/android-DirectorySelection
132
133 Patches are encouraged, and may be submitted by forking this project and
134 submitting a pull request through GitHub. Please see CONTRIBUTING.md for more details.
135
136 License
137 -------
138
139 Copyright 2014 The Android Open Source Project, Inc.
140
141 Licensed to the Apache Software Foundation (ASF) under one or more contributor
142 license agreements. See the NOTICE file distributed with this work for
143 additional information regarding copyright ownership. The ASF licenses this
144 file to you under the Apache License, Version 2.0 (the "License"); you may not
145 use this file except in compliance with the License. You may obtain a copy of
146 the License at
147
148 http://www.apache.org/licenses/LICENSE-2.0
149
150 Unless required by applicable law or agreed to in writing, software
151 distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
152 WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
153 License for the specific language governing permissions and limitations under
154 the License.
155