├── .gitignore ├── LICENSE ├── NOTICE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── hippo │ │ └── unifile │ │ └── example │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── assets │ │ ├── file.txt │ │ └── file2 │ │ │ ├── file3 │ │ │ └── hihi.txt │ │ │ └── haha │ ├── java │ │ └── com │ │ │ └── hippo │ │ │ └── unifile │ │ │ └── example │ │ │ ├── FileActivity.java │ │ │ ├── FileListActivity.java │ │ │ └── MainActivity.java │ └── res │ │ ├── layout │ │ ├── activity_file.xml │ │ ├── activity_file_list.xml │ │ └── activity_main.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ └── values │ │ └── strings.xml │ └── test │ └── java │ └── com │ └── hippo │ └── unifile │ └── example │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── library ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── hippo │ │ └── unifile │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ └── java │ │ └── com │ │ └── hippo │ │ └── unifile │ │ ├── AssetFile.java │ │ ├── Contracts.java │ │ ├── DocumentsContractApi19.java │ │ ├── DocumentsContractApi21.java │ │ ├── FilenameFilter.java │ │ ├── IOUtils.java │ │ ├── MediaContract.java │ │ ├── MediaFile.java │ │ ├── RawFile.java │ │ ├── RawRandomAccessFile.java │ │ ├── ResourceFile.java │ │ ├── ResourcesContract.java │ │ ├── SingleDocumentFile.java │ │ ├── TreeDocumentFile.java │ │ ├── TrickOutputStream.java │ │ ├── TrickRandomAccessFile.java │ │ ├── UniFile.java │ │ ├── UniRandomAccessFile.java │ │ ├── UriHandler.java │ │ └── Utils.java │ └── test │ └── java │ └── com │ └── hippo │ └── unifile │ └── ExampleUnitTest.java └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /local.properties 3 | .DS_Store 4 | /build 5 | /src 6 | *.iml 7 | .idea 8 | /captures 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | UniFile 2 | Copyright 2015-2016 Hippo Seven 3 | The UniFile is forked from `android.support.v4.provider.DocumentFile`, but more powerful. 4 | 5 | 香风智乃是最可爱的女孩子。 6 | chino kafuu is the cutest girl. 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # UniFile 2 | 3 | UniFile 是基于 `android.support.v4.provider.DocumentFile`,而且更好用。 4 | 5 | The UniFile is forked from `android.support.v4.provider.DocumentFile`, but more powerful. 6 | 7 | 区别有: 8 | * 添加其他文件 uri 支持 9 | * 支持所有 ContentProvider 的 uri 10 | * 支持 asset 文件的 uri,如 `file:///android_asset/text/uccu.txt` 11 | * 支持 resource 文件的 uri,如 `android.resource://com.hippo.unifile.example/2130903040` 12 | * 添加 getFilePath() 13 | * 添加 listFiles(FilenameFilter) 14 | * 添加 openOutputStream(),openOutputStream(boolean append),openInputStream() 15 | * 添加 createRandomAccessFile(String mode) 16 | * 删除了 createFile 中 mimeType 参数 17 | * 修改 createFile,createDirectory 特性,避免出现文件名后添加 (1) 的现象 18 | 19 | The differences: 20 | * Add other file uri support 21 | * Support all uri from ContentProvider 22 | * Support all asset file uri, like `file:///android_asset/text/uccu.txt` 23 | * Support all resource file uri, like `android.resource://com.hippo.unifile.example/2130903040` 24 | * Add getFilePath() 25 | * Add listFiles(FilenameFilter) 26 | * Add openOutputStream(),openOutputStream(boolean append),openInputStream() 27 | * Add createRandomAccessFile(String mode) 28 | * Remove mimeType in createFile function 29 | * Avoid filename ending with (1) in createFile,createDirectory 30 | 31 | 32 | # Usage 33 | 34 | 在最外面的 `build.gradle` 里加上 jitpack,别加到 buildscript 里了。 35 | 36 | Add jitpack repository in top `build.gradle`, DO **NOT** ADD IT TO buildscript. 37 | 38 | allprojects { 39 | repositories { 40 | ... 41 | maven { url "https://jitpack.io" } 42 | } 43 | } 44 | 45 | 在项目 `build.gradle` 里添加 UniFile 依赖。 46 | 47 | Add UniFile as dependency in project `build.gradle`. 48 | 49 | dependencies { 50 | ... 51 | compile 'com.github.seven332:unifile:1.0.0' 52 | } 53 | 54 | 在代码中使用: 55 | 56 | Use UniFile in your code: 57 | 58 | ```java 59 | // 从 Uri 创建 UniFile 60 | // Create UniFile from Uri 61 | file = UniFile.fromUri(context, uri); 62 | 63 | // 从 File 创建 UniFile 64 | // Create UniFile from File 65 | file = UniFile.fromFile(f); 66 | 67 | // 从 asset path 创建 UniFile 68 | // Create UniFile from asset path 69 | file = UniFile.fromAsset(assetManager, path); 70 | 71 | // 从 resource id 创建 UniFile 72 | // Create UniFile from resource id 73 | file = UniFile.fromResource(context, resId); 74 | 75 | // 获取原始文件路径 76 | // Get origin file path 77 | path = file.getFilePath(); 78 | 79 | // 创建随机访问文件 80 | // Create random access file 81 | raf = file.createRandomAccessFile("rw"); 82 | ``` 83 | 84 | # License 85 | 86 | Copyright (C) 2015-2016 Hippo Seven 87 | 88 | Licensed under the Apache License, Version 2.0 (the "License"); 89 | you may not use this file except in compliance with the License. 90 | You may obtain a copy of the License at 91 | 92 | http://www.apache.org/licenses/LICENSE-2.0 93 | 94 | Unless required by applicable law or agreed to in writing, software 95 | distributed under the License is distributed on an "AS IS" BASIS, 96 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 97 | See the License for the specific language governing permissions and 98 | limitations under the License. 99 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | 3 | # Intellij 4 | *.iml 5 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.1" 6 | 7 | defaultConfig { 8 | applicationId "com.hippo.unifile.example" 9 | minSdkVersion 4 10 | targetSdkVersion 25 11 | versionCode 7 12 | versionName '1.0.0' 13 | } 14 | 15 | buildTypes { 16 | release { 17 | minifyEnabled false 18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 19 | } 20 | } 21 | } 22 | 23 | dependencies { 24 | compile fileTree(dir: 'libs', include: ['*.jar']) 25 | compile project(':library') 26 | testCompile 'junit:junit:4.12' 27 | } 28 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in E:\Android\android-sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/hippo/unifile/example/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.hippo.unifile.example; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /app/src/main/assets/file.txt: -------------------------------------------------------------------------------- 1 | dsadsafewgfdb xcvbadscdx sd 2 | dsagfvds 3 | -------------------------------------------------------------------------------- /app/src/main/assets/file2/file3/hihi.txt: -------------------------------------------------------------------------------- 1 | 2 | hihih 3 | hih -------------------------------------------------------------------------------- /app/src/main/assets/file2/haha: -------------------------------------------------------------------------------- 1 | 2 | hahah 3 | hah -------------------------------------------------------------------------------- /app/src/main/java/com/hippo/unifile/example/FileActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Hippo Seven 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.hippo.unifile.example; 18 | 19 | /* 20 | * Created by Hippo on 11/20/2016. 21 | */ 22 | 23 | import android.app.Activity; 24 | import android.content.Intent; 25 | import android.net.Uri; 26 | import android.os.Bundle; 27 | import android.util.Log; 28 | import android.view.View; 29 | import android.widget.EditText; 30 | import android.widget.TextView; 31 | import android.widget.Toast; 32 | 33 | import com.hippo.unifile.FilenameFilter; 34 | import com.hippo.unifile.UniFile; 35 | import com.hippo.unifile.UniRandomAccessFile; 36 | 37 | import java.io.IOException; 38 | import java.io.InputStream; 39 | import java.io.OutputStream; 40 | import java.util.ArrayList; 41 | 42 | public class FileActivity extends Activity { 43 | 44 | private Uri mUri; 45 | private UniFile mFile; 46 | 47 | @Override 48 | protected void onCreate(Bundle savedInstanceState) { 49 | super.onCreate(savedInstanceState); 50 | 51 | Intent intent = getIntent(); 52 | if (intent == null) { 53 | Toast.makeText(this, "No data", Toast.LENGTH_SHORT).show(); 54 | finish(); 55 | return; 56 | } 57 | mUri = intent.getData(); 58 | if (mUri == null) { 59 | Toast.makeText(this, "No data", Toast.LENGTH_SHORT).show(); 60 | finish(); 61 | return; 62 | } 63 | 64 | mFile = UniFile.fromUri(this, mUri); 65 | if (mFile == null) { 66 | Log.w("TAG", "Can't recognize the uri: " + mUri); 67 | Toast.makeText(this, "Can't recognize the uri", Toast.LENGTH_SHORT).show(); 68 | finish(); 69 | return; 70 | } 71 | 72 | setContentView(R.layout.activity_file); 73 | 74 | findViewById(R.id.refresh_detail).setOnClickListener(new View.OnClickListener() { 75 | @Override 76 | public void onClick(View view) { 77 | refreshDetail(); 78 | } 79 | }); 80 | findViewById(R.id.delete).setOnClickListener(new View.OnClickListener() { 81 | @Override 82 | public void onClick(View view) { 83 | delete(); 84 | } 85 | }); 86 | findViewById(R.id.list_files).setOnClickListener(new View.OnClickListener() { 87 | @Override 88 | public void onClick(View view) { 89 | listFiles(); 90 | } 91 | }); 92 | findViewById(R.id.list_files_filename_filter).setOnClickListener(new View.OnClickListener() { 93 | @Override 94 | public void onClick(View view) { 95 | listFilesFilenameFilter(); 96 | } 97 | }); 98 | findViewById(R.id.find_file).setOnClickListener(new View.OnClickListener() { 99 | @Override 100 | public void onClick(View view) { 101 | findFile(); 102 | } 103 | }); 104 | findViewById(R.id.create_file).setOnClickListener(new View.OnClickListener() { 105 | @Override 106 | public void onClick(View view) { 107 | createFile(); 108 | } 109 | }); 110 | findViewById(R.id.create_directory).setOnClickListener(new View.OnClickListener() { 111 | @Override 112 | public void onClick(View view) { 113 | createDirectory(); 114 | } 115 | }); 116 | findViewById(R.id.open_output_stream).setOnClickListener(new View.OnClickListener() { 117 | @Override 118 | public void onClick(View view) { 119 | openOutputStream(); 120 | } 121 | }); 122 | findViewById(R.id.open_input_stream).setOnClickListener(new View.OnClickListener() { 123 | @Override 124 | public void onClick(View view) { 125 | openInputStream(); 126 | } 127 | }); 128 | findViewById(R.id.create_random_access_file_r).setOnClickListener(new View.OnClickListener() { 129 | @Override 130 | public void onClick(View view) { 131 | createRandomAccessFileR(); 132 | } 133 | }); 134 | findViewById(R.id.create_random_access_file_rw).setOnClickListener(new View.OnClickListener() { 135 | @Override 136 | public void onClick(View view) { 137 | createRandomAccessFileRW(); 138 | } 139 | }); 140 | 141 | refreshDetail(); 142 | } 143 | 144 | private void refreshDetail() { 145 | TextView detail = (TextView) findViewById(R.id.detail); 146 | StringBuilder sb = new StringBuilder(); 147 | sb.append("Uri: ").append(mUri.toString()).append("\n"); 148 | sb.append("Class: ").append(mFile.getClass().getName()).append("\n"); 149 | sb.append("getUri(): ").append(mFile.getUri()).append("\n"); 150 | sb.append("getName(): ").append(mFile.getName()).append("\n"); 151 | sb.append("getType(): ").append(mFile.getType()).append("\n"); 152 | sb.append("getFilePath(): ").append(mFile.getFilePath()).append("\n"); 153 | sb.append("isDirectory(): ").append(mFile.isDirectory()).append("\n"); 154 | sb.append("isFile(): ").append(mFile.isFile()).append("\n"); 155 | sb.append("lastModified(): ").append(mFile.lastModified()).append("\n"); 156 | sb.append("length(): ").append(mFile.length()).append("\n"); 157 | sb.append("canRead(): ").append(mFile.canRead()).append("\n"); 158 | sb.append("canWrite(): ").append(mFile.canWrite()).append("\n"); 159 | sb.append("exists(): ").append(mFile.exists()).append("\n"); 160 | detail.setText(sb.toString()); 161 | } 162 | 163 | private void delete() { 164 | ((TextView) findViewById(R.id.delete_result)).setText("" + mFile.delete()); 165 | } 166 | 167 | private void listFiles() { 168 | UniFile[] files = mFile.listFiles(); 169 | if (files == null) { 170 | Toast.makeText(this, "null", Toast.LENGTH_SHORT).show(); 171 | } else if (files.length == 0) { 172 | Toast.makeText(this, "empty", Toast.LENGTH_SHORT).show(); 173 | } else { 174 | ArrayList uris = new ArrayList<>(); 175 | for (UniFile f : files) { 176 | uris.add(f.getUri()); 177 | } 178 | Intent intent = new Intent(this, FileListActivity.class); 179 | intent.putParcelableArrayListExtra(FileListActivity.KEY_URIS, uris); 180 | startActivity(intent); 181 | } 182 | } 183 | 184 | private void listFilesFilenameFilter() { 185 | UniFile[] files = mFile.listFiles(new FilenameFilter() { 186 | @Override 187 | public boolean accept(UniFile dir, String filename) { 188 | return filename.startsWith("a"); 189 | } 190 | }); 191 | if (files == null) { 192 | Toast.makeText(this, "null", Toast.LENGTH_SHORT).show(); 193 | } else if (files.length == 0) { 194 | Toast.makeText(this, "empty", Toast.LENGTH_SHORT).show(); 195 | } else { 196 | ArrayList uris = new ArrayList<>(); 197 | for (UniFile f : files) { 198 | uris.add(f.getUri()); 199 | } 200 | Intent intent = new Intent(this, FileListActivity.class); 201 | intent.putParcelableArrayListExtra(FileListActivity.KEY_URIS, uris); 202 | startActivity(intent); 203 | } 204 | } 205 | 206 | private void findFile() { 207 | String filename = ((EditText) findViewById(R.id.find_file_param)).getText().toString(); 208 | UniFile file = mFile.findFile(filename); 209 | if (file != null) { 210 | Intent intent = new Intent(this, FileActivity.class); 211 | intent.setData(file.getUri()); 212 | startActivity(intent); 213 | } else { 214 | Toast.makeText(this, "null", Toast.LENGTH_SHORT).show(); 215 | } 216 | } 217 | 218 | private void createFile() { 219 | String filename = ((EditText) findViewById(R.id.create_file_param)).getText().toString(); 220 | UniFile file = mFile.createFile(filename); 221 | if (file != null) { 222 | Intent intent = new Intent(this, FileActivity.class); 223 | intent.setData(file.getUri()); 224 | startActivity(intent); 225 | } else { 226 | Toast.makeText(this, "null", Toast.LENGTH_SHORT).show(); 227 | } 228 | } 229 | 230 | private void createDirectory() { 231 | String filename = ((EditText) findViewById(R.id.create_directory_param)).getText().toString(); 232 | UniFile file = mFile.createDirectory(filename); 233 | if (file != null) { 234 | Intent intent = new Intent(this, FileActivity.class); 235 | intent.setData(file.getUri()); 236 | startActivity(intent); 237 | } else { 238 | Toast.makeText(this, "null", Toast.LENGTH_SHORT).show(); 239 | } 240 | } 241 | 242 | private void openOutputStream() { 243 | boolean result; 244 | OutputStream os = null; 245 | try { 246 | os = mFile.openOutputStream(); 247 | result = true; 248 | } catch (IOException e) { 249 | result = false; 250 | } finally { 251 | if (os != null) { 252 | try { 253 | os.close(); 254 | } catch (IOException e) { 255 | e.printStackTrace(); 256 | } 257 | } 258 | } 259 | ((TextView) findViewById(R.id.open_output_stream_result)).setText("" + result); 260 | } 261 | 262 | private void openInputStream() { 263 | boolean result; 264 | InputStream is = null; 265 | try { 266 | is = mFile.openInputStream(); 267 | result = true; 268 | } catch (IOException e) { 269 | result = false; 270 | } finally { 271 | if (is != null) { 272 | try { 273 | is.close(); 274 | } catch (IOException e) { 275 | e.printStackTrace(); 276 | } 277 | } 278 | } 279 | ((TextView) findViewById(R.id.open_input_stream_result)).setText("" + result); 280 | } 281 | 282 | private void createRandomAccessFileR() { 283 | boolean result; 284 | UniRandomAccessFile uraf = null; 285 | try { 286 | uraf = mFile.createRandomAccessFile("r"); 287 | result = true; 288 | } catch (IOException e) { 289 | result = false; 290 | } finally { 291 | if (uraf != null) { 292 | try { 293 | uraf.close(); 294 | } catch (IOException e) { 295 | e.printStackTrace(); 296 | } 297 | } 298 | } 299 | ((TextView) findViewById(R.id.create_random_access_file_r_result)).setText("" + result); 300 | } 301 | 302 | private void createRandomAccessFileRW() { 303 | boolean result; 304 | UniRandomAccessFile uraf = null; 305 | try { 306 | uraf = mFile.createRandomAccessFile("rw"); 307 | result = true; 308 | } catch (IOException e) { 309 | result = false; 310 | } finally { 311 | if (uraf != null) { 312 | try { 313 | uraf.close(); 314 | } catch (IOException e) { 315 | e.printStackTrace(); 316 | } 317 | } 318 | } 319 | ((TextView) findViewById(R.id.create_random_access_file_rw_result)).setText("" + result); 320 | } 321 | } 322 | -------------------------------------------------------------------------------- /app/src/main/java/com/hippo/unifile/example/FileListActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Hippo Seven 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.hippo.unifile.example; 18 | 19 | /* 20 | * Created by Hippo on 11/25/2016. 21 | */ 22 | 23 | import android.app.Activity; 24 | import android.content.Intent; 25 | import android.net.Uri; 26 | import android.os.Bundle; 27 | import android.view.View; 28 | import android.view.ViewGroup; 29 | import android.widget.AdapterView; 30 | import android.widget.BaseAdapter; 31 | import android.widget.ListView; 32 | import android.widget.TextView; 33 | import android.widget.Toast; 34 | 35 | import com.hippo.unifile.UniFile; 36 | 37 | import java.util.ArrayList; 38 | import java.util.List; 39 | 40 | public class FileListActivity extends Activity { 41 | 42 | public static final String KEY_URIS = "uris"; 43 | 44 | @Override 45 | protected void onCreate(Bundle savedInstanceState) { 46 | super.onCreate(savedInstanceState); 47 | 48 | Intent intent = getIntent(); 49 | if (intent == null) { 50 | Toast.makeText(this, "No data", Toast.LENGTH_SHORT).show(); 51 | finish(); 52 | return; 53 | } 54 | 55 | ArrayList uris = intent.getParcelableArrayListExtra(KEY_URIS); 56 | if (uris == null) { 57 | Toast.makeText(this, "No data", Toast.LENGTH_SHORT).show(); 58 | finish(); 59 | return; 60 | } 61 | 62 | final List files = new ArrayList<>(); 63 | for (Uri uri : uris) { 64 | UniFile f = UniFile.fromUri(this, uri); 65 | if (f == null) { 66 | throw new IllegalStateException("Can't convert Uri to UniFile: " + uri); 67 | } 68 | files.add(f); 69 | } 70 | 71 | setContentView(R.layout.activity_file_list); 72 | 73 | ListView listView = (ListView) findViewById(R.id.file_list); 74 | listView.setAdapter(new BaseAdapter() { 75 | @Override 76 | public int getCount() { 77 | return files.size(); 78 | } 79 | 80 | @Override 81 | public Object getItem(int i) { 82 | return files.get(i); 83 | } 84 | 85 | @Override 86 | public long getItemId(int i) { 87 | return files.get(i).hashCode(); 88 | } 89 | 90 | @Override 91 | public View getView(int i, View view, ViewGroup viewGroup) { 92 | if (view == null) { 93 | view = getLayoutInflater().inflate(android.R.layout.simple_list_item_2, viewGroup, false); 94 | } 95 | ((TextView) ((ViewGroup) view).getChildAt(0)).setText(files.get(i).getName()); 96 | ((TextView) ((ViewGroup) view).getChildAt(1)).setText(files.get(i).getUri().toString()); 97 | return view; 98 | } 99 | }); 100 | listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 101 | @Override 102 | public void onItemClick(AdapterView adapterView, View view, int i, long l) { 103 | Intent intent = new Intent(FileListActivity.this, FileActivity.class); 104 | intent.setData(files.get(i).getUri()); 105 | startActivity(intent); 106 | } 107 | }); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /app/src/main/java/com/hippo/unifile/example/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.hippo.unifile.example; 2 | 3 | import android.Manifest; 4 | import android.annotation.TargetApi; 5 | import android.app.Activity; 6 | import android.content.ActivityNotFoundException; 7 | import android.content.Intent; 8 | import android.content.pm.PackageManager; 9 | import android.net.Uri; 10 | import android.os.Build; 11 | import android.os.Bundle; 12 | import android.os.Environment; 13 | import android.support.annotation.NonNull; 14 | import android.view.View; 15 | import android.widget.Toast; 16 | 17 | import com.hippo.unifile.UniFile; 18 | 19 | import java.io.File; 20 | 21 | public class MainActivity extends Activity { 22 | 23 | @Override 24 | protected void onCreate(Bundle savedInstanceState) { 25 | super.onCreate(savedInstanceState); 26 | setContentView(R.layout.activity_main); 27 | 28 | findViewById(R.id.action_pick).setOnClickListener(new View.OnClickListener() { 29 | @Override 30 | public void onClick(View view) { 31 | Uri uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI; 32 | Intent intent = new Intent(Intent.ACTION_PICK, uri); 33 | startActivityForResult(intent, 0); 34 | } 35 | }); 36 | 37 | findViewById(R.id.action_get_content).setOnClickListener(new View.OnClickListener() { 38 | @Override 39 | public void onClick(View view) { 40 | Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 41 | intent.setType("*/*"); 42 | startActivityForResult(intent, 0); 43 | } 44 | }); 45 | 46 | findViewById(R.id.action_open_document).setOnClickListener(new View.OnClickListener() { 47 | @Override 48 | public void onClick(View view) { 49 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 50 | Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); 51 | intent.setType("*/*"); 52 | startActivityForResult(intent, 0); 53 | } else { 54 | Toast.makeText(MainActivity.this, "Only supported in Android4.4+", Toast.LENGTH_SHORT).show(); 55 | } 56 | } 57 | }); 58 | 59 | findViewById(R.id.action_open_document_tree).setOnClickListener(new View.OnClickListener() { 60 | @Override 61 | public void onClick(View view) { 62 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 63 | Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); 64 | startActivityForResult(intent, 0); 65 | } else { 66 | Toast.makeText(MainActivity.this, "Only supported in Android5.0+", Toast.LENGTH_SHORT).show(); 67 | } 68 | } 69 | }); 70 | 71 | findViewById(R.id.asset).setOnClickListener(new View.OnClickListener() { 72 | @Override 73 | public void onClick(View view) { 74 | startFileActivity(UniFile.fromAsset(getAssets(), "file2").getUri()); 75 | } 76 | }); 77 | 78 | findViewById(R.id.resource).setOnClickListener(new View.OnClickListener() { 79 | @Override 80 | public void onClick(View view) { 81 | startFileActivity(UniFile.fromResource(MainActivity.this, R.layout.activity_file).getUri()); 82 | } 83 | }); 84 | 85 | findViewById(R.id.raw).setOnClickListener(new View.OnClickListener() { 86 | @Override 87 | public void onClick(View view) { 88 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 89 | onClickRawApi23(); 90 | } else { 91 | onClickRaw(); 92 | } 93 | } 94 | }); 95 | } 96 | 97 | @TargetApi(Build.VERSION_CODES.M) 98 | private void onClickRawApi23() { 99 | if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { 100 | if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { 101 | Toast.makeText(this, "Grant the permission to test raw file.", Toast.LENGTH_SHORT).show(); 102 | } 103 | requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0); 104 | } else { 105 | startFileActivityFromRaw(); 106 | } 107 | } 108 | 109 | @Override 110 | public void onRequestPermissionsResult(int requestCode, 111 | @NonNull String permissions[], @NonNull int[] grantResults) { 112 | if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 113 | startFileActivityFromRaw(); 114 | } else { 115 | Toast.makeText(this, "Permission denied.", Toast.LENGTH_SHORT).show(); 116 | } 117 | } 118 | 119 | private void onClickRaw() { 120 | startFileActivityFromRaw(); 121 | } 122 | 123 | private void startFileActivityFromRaw() { 124 | File file = Environment.getExternalStorageDirectory(); 125 | if (file != null) { 126 | startFileActivity(Uri.fromFile(file)); 127 | } else { 128 | Toast.makeText(this, "Can't find external storage.", Toast.LENGTH_SHORT).show(); 129 | } 130 | } 131 | 132 | @Override 133 | public void onActivityResult(int requestCode, int resultCode, Intent data) { 134 | if (resultCode != Activity.RESULT_OK) { 135 | return; 136 | } 137 | startFileActivity(data.getData()); 138 | } 139 | 140 | @Override 141 | public void startActivityForResult(Intent intent, int requestCode) { 142 | try { 143 | super.startActivityForResult(intent, 0); 144 | } catch (ActivityNotFoundException e) { 145 | Toast.makeText(MainActivity.this, "error_cant_find_activity", Toast.LENGTH_SHORT).show(); 146 | } 147 | } 148 | 149 | private void startFileActivity(Uri uri) { 150 | Intent intent = new Intent(this, FileActivity.class); 151 | intent.setData(uri); 152 | startActivity(intent); 153 | } 154 | } 155 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_file.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 22 | 23 | 27 | 28 |