├── app ├── src │ ├── main │ │ ├── assets │ │ │ ├── file2 │ │ │ │ ├── haha │ │ │ │ └── file3 │ │ │ │ │ └── hihi.txt │ │ │ └── file.txt │ │ ├── res │ │ │ ├── values │ │ │ │ └── strings.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 │ │ │ └── layout │ │ │ │ ├── activity_file_list.xml │ │ │ │ ├── activity_main.xml │ │ │ │ └── activity_file.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── com │ │ │ └── hippo │ │ │ └── unifile │ │ │ └── example │ │ │ ├── FileListActivity.java │ │ │ ├── MainActivity.java │ │ │ └── FileActivity.java │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── hippo │ │ │ └── unifile │ │ │ └── example │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── hippo │ │ └── unifile │ │ └── example │ │ └── ApplicationTest.java ├── .gitignore ├── build.gradle └── proguard-rules.pro ├── settings.gradle ├── library ├── .gitignore ├── src │ ├── main │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── com │ │ │ └── hippo │ │ │ └── unifile │ │ │ ├── IOUtils.java │ │ │ ├── UriHandler.java │ │ │ ├── FilenameFilter.java │ │ │ ├── MediaContract.java │ │ │ ├── RawRandomAccessFile.java │ │ │ ├── TrickOutputStream.java │ │ │ ├── Contracts.java │ │ │ ├── ResourcesContract.java │ │ │ ├── Utils.java │ │ │ ├── UniRandomAccessFile.java │ │ │ ├── ResourceFile.java │ │ │ ├── DocumentsContractApi21.java │ │ │ ├── MediaFile.java │ │ │ ├── SingleDocumentFile.java │ │ │ ├── TrickRandomAccessFile.java │ │ │ ├── RawFile.java │ │ │ ├── AssetFile.java │ │ │ ├── TreeDocumentFile.java │ │ │ ├── DocumentsContractApi19.java │ │ │ └── UniFile.java │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── hippo │ │ │ └── unifile │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── hippo │ │ └── unifile │ │ └── ApplicationTest.java ├── proguard-rules.pro └── build.gradle ├── .gitignore ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── NOTICE ├── gradle.properties ├── gradlew.bat ├── README.md ├── gradlew └── LICENSE /app/src/main/assets/file2/haha: -------------------------------------------------------------------------------- 1 | 2 | hahah 3 | hah -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':library' 2 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | 3 | # Intellij 4 | *.iml 5 | -------------------------------------------------------------------------------- /app/src/main/assets/file2/file3/hihi.txt: -------------------------------------------------------------------------------- 1 | 2 | hihih 3 | hih -------------------------------------------------------------------------------- /library/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | 3 | # Intellij 4 | *.iml 5 | -------------------------------------------------------------------------------- /app/src/main/assets/file.txt: -------------------------------------------------------------------------------- 1 | dsadsafewgfdb xcvbadscdx sd 2 | dsagfvds 3 | -------------------------------------------------------------------------------- /library/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /local.properties 3 | .DS_Store 4 | /build 5 | /src 6 | *.iml 7 | .idea 8 | /captures 9 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | UniFile 3 | 4 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seven332/UniFile/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seven332/UniFile/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seven332/UniFile/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seven332/UniFile/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seven332/UniFile/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seven332/UniFile/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Dec 28 10:00:20 PST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip 7 | -------------------------------------------------------------------------------- /library/src/test/java/com/hippo/unifile/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.hippo.unifile; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * To work on unit tests, switch the Test Artifact in the Build Variants view. 9 | */ 10 | public class ExampleUnitTest { 11 | @Test 12 | public void addition_isCorrect() throws Exception { 13 | assertEquals(4, 2 + 2); 14 | } 15 | } -------------------------------------------------------------------------------- /app/src/test/java/com/hippo/unifile/example/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.hippo.unifile.example; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * To work on unit tests, switch the Test Artifact in the Build Variants view. 9 | */ 10 | public class ExampleUnitTest { 11 | @Test 12 | public void addition_isCorrect() throws Exception { 13 | assertEquals(4, 2 + 2); 14 | } 15 | } -------------------------------------------------------------------------------- /library/src/androidTest/java/com/hippo/unifile/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.hippo.unifile; 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/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 | } -------------------------------------------------------------------------------- /library/src/main/java/com/hippo/unifile/IOUtils.java: -------------------------------------------------------------------------------- 1 | package com.hippo.unifile; 2 | 3 | import java.io.Closeable; 4 | import java.io.IOException; 5 | 6 | class IOUtils { 7 | 8 | /** 9 | * Close the closeable stuff. Don't worry about anything. 10 | * 11 | * @param is the closeable stuff 12 | */ 13 | public static void closeQuietly(Closeable is) { 14 | if (is != null) { 15 | try { 16 | is.close(); 17 | } catch (IOException e) { 18 | // Ignore 19 | } 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /library/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 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true 19 | -------------------------------------------------------------------------------- /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/res/layout/activity_file_list.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /library/src/main/java/com/hippo/unifile/UriHandler.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; 18 | 19 | /* 20 | * Created by Hippo on 8/16/2016. 21 | */ 22 | 23 | import android.content.Context; 24 | import android.net.Uri; 25 | 26 | /** 27 | * A UriHandler is to get UniFile from custom uri for extensions 28 | */ 29 | public interface UriHandler { 30 | 31 | /** 32 | * Create a {@link UniFile} representing the uri 33 | */ 34 | UniFile fromUri(Context context, Uri uri); 35 | } 36 | -------------------------------------------------------------------------------- /library/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'com.github.dcendents.android-maven' 3 | 4 | group='com.github.seven332' 5 | 6 | android { 7 | compileSdkVersion 25 8 | buildToolsVersion "25.0.1" 9 | 10 | defaultConfig { 11 | minSdkVersion 4 12 | targetSdkVersion 25 13 | versionCode 7 14 | versionName '1.0.0' 15 | } 16 | 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | } 24 | 25 | dependencies { 26 | compile fileTree(dir: 'libs', include: ['*.jar']) 27 | testCompile 'junit:junit:4.12' 28 | compile 'com.android.support:support-annotations:25.0.1' 29 | } 30 | 31 | // build a jar with source files 32 | task sourcesJar(type: Jar) { 33 | from android.sourceSets.main.java.srcDirs 34 | classifier = 'sources' 35 | } 36 | 37 | task javadoc(type: Javadoc) { 38 | failOnError false 39 | source = android.sourceSets.main.java.sourceFiles 40 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) 41 | classpath += configurations.compile 42 | } 43 | 44 | // build a jar with javadoc 45 | task javadocJar(type: Jar, dependsOn: javadoc) { 46 | classifier = 'javadoc' 47 | from javadoc.destinationDir 48 | } 49 | 50 | artifacts { 51 | archives sourcesJar 52 | archives javadocJar 53 | } 54 | -------------------------------------------------------------------------------- /library/src/main/java/com/hippo/unifile/FilenameFilter.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; 18 | 19 | /** 20 | * An interface for filtering {@link UniFile} objects based on their names 21 | * or the directory they reside in. 22 | * 23 | * @see UniFile 24 | * @see UniFile#listFiles(FilenameFilter) 25 | */ 26 | public interface FilenameFilter { 27 | 28 | /** 29 | * Indicates if a specific filename matches this filter. 30 | * 31 | * @param dir 32 | * the directory in which the {@code filename} was found. 33 | * @param filename 34 | * the name of the file in {@code dir} to test. 35 | * @return {@code true} if the filename matches the filter 36 | * and can be included in the list, {@code false} 37 | * otherwise. 38 | */ 39 | boolean accept(UniFile dir, String filename); 40 | } 41 | -------------------------------------------------------------------------------- /library/src/main/java/com/hippo/unifile/MediaContract.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; 18 | 19 | import android.content.Context; 20 | import android.net.Uri; 21 | import android.provider.MediaStore; 22 | 23 | final class MediaContract { 24 | private MediaContract() {} 25 | 26 | public static String getName(Context context, Uri self) { 27 | return Contracts.queryForString(context, self, MediaStore.MediaColumns.DISPLAY_NAME, null); 28 | } 29 | 30 | public static String getType(Context context, Uri self) { 31 | return Contracts.queryForString(context, self, MediaStore.MediaColumns.MIME_TYPE, null); 32 | } 33 | 34 | public static String getFilePath(Context context, Uri self) { 35 | return Contracts.queryForString(context, self, MediaStore.MediaColumns.DATA, null); 36 | } 37 | 38 | public static long lastModified(Context context, Uri self) { 39 | return Contracts.queryForLong(context, self, MediaStore.MediaColumns.DATE_MODIFIED, -1L); 40 | } 41 | 42 | public static long length(Context context, Uri self) { 43 | return Contracts.queryForLong(context, self, MediaStore.MediaColumns.SIZE, -1L); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /library/src/main/java/com/hippo/unifile/RawRandomAccessFile.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; 18 | 19 | /* 20 | * Created by Hippo on 8/15/2016. 21 | */ 22 | 23 | import java.io.IOException; 24 | import java.io.RandomAccessFile; 25 | 26 | class RawRandomAccessFile implements UniRandomAccessFile { 27 | 28 | private RandomAccessFile mFile; 29 | 30 | RawRandomAccessFile(RandomAccessFile file) { 31 | mFile = file; 32 | } 33 | 34 | @Override 35 | public void close() throws IOException { 36 | mFile.close(); 37 | } 38 | 39 | @Override 40 | public long getFilePointer() throws IOException { 41 | return mFile.getFilePointer(); 42 | } 43 | 44 | @Override 45 | public void seek(long pos) throws IOException { 46 | mFile.seek(pos); 47 | } 48 | 49 | @Override 50 | public int skipBytes(int n) throws IOException { 51 | return mFile.skipBytes(n); 52 | } 53 | 54 | @Override 55 | public long length() throws IOException { 56 | return mFile.length(); 57 | } 58 | 59 | @Override 60 | public void setLength(long newLength) throws IOException { 61 | mFile.setLength(newLength); 62 | } 63 | 64 | @Override 65 | public void read(byte[] b) throws IOException { 66 | mFile.read(b); 67 | } 68 | 69 | @Override 70 | public void read(byte[] b, int off, int len) throws IOException { 71 | mFile.read(b, off, len); 72 | } 73 | 74 | @Override 75 | public void write(byte[] b) throws IOException { 76 | mFile.write(b); 77 | } 78 | 79 | @Override 80 | public void write(byte[] b, int off, int len) throws IOException { 81 | mFile.write(b, off, len); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /library/src/main/java/com/hippo/unifile/TrickOutputStream.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; 18 | 19 | /* 20 | * Created by Hippo on 8/15/2016. 21 | */ 22 | 23 | import android.content.Context; 24 | import android.net.Uri; 25 | import android.os.ParcelFileDescriptor; 26 | import android.support.annotation.NonNull; 27 | 28 | import java.io.FileDescriptor; 29 | import java.io.FileOutputStream; 30 | import java.io.IOException; 31 | import java.io.OutputStream; 32 | 33 | // The OutputStream from Context.getContentResolver().openOutputStream() 34 | // and FileProvider uri may throw Exception when write. The Exception looks like: 35 | // java.io.IOException: write failed: EBADF (Bad file descriptor) 36 | // But TrickOutputStream can avoid it on my Nexus 5 cm13. 37 | // TODO need more test 38 | class TrickOutputStream extends FileOutputStream { 39 | 40 | private final ParcelFileDescriptor mPfd; 41 | 42 | private TrickOutputStream(ParcelFileDescriptor pfd, FileDescriptor fd) { 43 | super(fd); 44 | mPfd = pfd; 45 | } 46 | 47 | @Override 48 | public void close() throws IOException { 49 | mPfd.close(); 50 | super.close(); 51 | } 52 | 53 | @NonNull 54 | static OutputStream create(Context context, Uri uri, String mode) throws IOException { 55 | ParcelFileDescriptor pfd; 56 | try { 57 | pfd = context.getContentResolver().openFileDescriptor(uri, mode); 58 | } catch (Exception e) { 59 | throw new IOException("Can't get ParcelFileDescriptor"); 60 | } 61 | if (pfd == null) { 62 | throw new IOException("Can't get ParcelFileDescriptor"); 63 | } 64 | FileDescriptor fd = pfd.getFileDescriptor(); 65 | if (fd == null) { 66 | throw new IOException("Can't get FileDescriptor"); 67 | } 68 | 69 | return new TrickOutputStream(pfd, fd); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /library/src/main/java/com/hippo/unifile/Contracts.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; 18 | 19 | import android.content.ContentResolver; 20 | import android.content.Context; 21 | import android.database.Cursor; 22 | import android.net.Uri; 23 | 24 | final class Contracts { 25 | private Contracts() {} 26 | 27 | public static String queryForString(Context context, Uri self, String column, 28 | String defaultValue) { 29 | final ContentResolver resolver = context.getContentResolver(); 30 | 31 | Cursor c = null; 32 | try { 33 | c = resolver.query(self, new String[] { column }, null, null, null); 34 | if (c != null && c.moveToFirst() && !c.isNull(0)) { 35 | return c.getString(0); 36 | } else { 37 | return defaultValue; 38 | } 39 | } catch (Exception e) { 40 | return defaultValue; 41 | } finally { 42 | closeQuietly(c); 43 | } 44 | } 45 | 46 | public static int queryForInt(Context context, Uri self, String column, 47 | int defaultValue) { 48 | return (int) queryForLong(context, self, column, defaultValue); 49 | } 50 | 51 | public static long queryForLong(Context context, Uri self, String column, 52 | long defaultValue) { 53 | final ContentResolver resolver = context.getContentResolver(); 54 | 55 | Cursor c = null; 56 | try { 57 | c = resolver.query(self, new String[] { column }, null, null, null); 58 | if (c != null && c.moveToFirst() && !c.isNull(0)) { 59 | return c.getLong(0); 60 | } else { 61 | return defaultValue; 62 | } 63 | } catch (Exception e) { 64 | return defaultValue; 65 | } finally { 66 | closeQuietly(c); 67 | } 68 | } 69 | 70 | public static void closeQuietly(Cursor closeable) { 71 | if (closeable != null) { 72 | try { 73 | closeable.close(); 74 | } catch (RuntimeException rethrown) { 75 | throw rethrown; 76 | } catch (Exception ignored) { 77 | } 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /library/src/main/java/com/hippo/unifile/ResourcesContract.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; 18 | 19 | /* 20 | * Created by Hippo on 11/23/2016. 21 | */ 22 | 23 | import android.content.Context; 24 | import android.content.pm.PackageManager; 25 | import android.content.res.Resources; 26 | import android.net.Uri; 27 | import android.text.TextUtils; 28 | 29 | import java.util.List; 30 | 31 | final class ResourcesContract { 32 | private ResourcesContract() {} 33 | 34 | public static OpenResourceResult openResource(Context context, Uri uri) { 35 | String authority = uri.getAuthority(); 36 | Resources r; 37 | if (TextUtils.isEmpty(authority)) { 38 | return null; 39 | } else { 40 | try { 41 | r = context.getPackageManager().getResourcesForApplication(authority); 42 | } catch (PackageManager.NameNotFoundException ex) { 43 | return null; 44 | } 45 | } 46 | List path = uri.getPathSegments(); 47 | if (path == null) { 48 | return null; 49 | } 50 | int len = path.size(); 51 | int id; 52 | String name; 53 | if (len == 1) { 54 | try { 55 | id = Integer.parseInt(path.get(0)); 56 | } catch (NumberFormatException e) { 57 | return null; 58 | } 59 | try { 60 | name = r.getResourceEntryName(id); 61 | } catch (Resources.NotFoundException e) { 62 | return null; 63 | } 64 | } else if (len == 2) { 65 | name = path.get(1); 66 | id = r.getIdentifier(path.get(1), path.get(0), authority); 67 | } else { 68 | return null; 69 | } 70 | if (id == 0 || name == null) { 71 | return null; 72 | } 73 | OpenResourceResult res = new OpenResourceResult(); 74 | res.r = r; 75 | res.p = authority; 76 | res.id = id; 77 | res.name = name; 78 | return res; 79 | } 80 | 81 | public static class OpenResourceResult { 82 | public Resources r; 83 | public String p; 84 | public int id; 85 | public String name; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /library/src/main/java/com/hippo/unifile/Utils.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; 18 | 19 | /* 20 | * Created by Hippo on 11/19/2016. 21 | */ 22 | 23 | import android.support.annotation.Nullable; 24 | import android.text.TextUtils; 25 | import android.webkit.MimeTypeMap; 26 | 27 | class Utils { 28 | private Utils() {} 29 | 30 | @Nullable 31 | static String getTypeForName(String name) { 32 | if (TextUtils.isEmpty(name)) { 33 | return null; 34 | } 35 | 36 | final int lastDot = name.lastIndexOf('.'); 37 | if (lastDot >= 0) { 38 | final String extension = name.substring(lastDot + 1).toLowerCase(); 39 | final String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); 40 | if (!TextUtils.isEmpty(mime)) { 41 | return mime; 42 | } 43 | } 44 | 45 | return "application/octet-stream"; 46 | } 47 | 48 | /** 49 | * A normal Unix pathname does not contain consecutive slashes and does not end 50 | * with a slash. The empty string and "/" are special cases that are also 51 | * considered normal. 52 | */ 53 | static String normalize(String pathname) { 54 | int n = pathname.length(); 55 | char[] normalized = pathname.toCharArray(); 56 | int index = 0; 57 | char prevChar = 0; 58 | for (int i = 0; i < n; i++) { 59 | char current = normalized[i]; 60 | // Remove duplicate slashes. 61 | if (!(current == '/' && prevChar == '/')) { 62 | normalized[index++] = current; 63 | } 64 | 65 | prevChar = current; 66 | } 67 | 68 | // Omit the trailing slash, except when pathname == "/". 69 | if (prevChar == '/' && n > 1) { 70 | index--; 71 | } 72 | 73 | return (index != n) ? new String(normalized, 0, index) : pathname; 74 | } 75 | 76 | // Invariant: Both |parent| and |child| are normalized paths. 77 | static String resolve(String parent, String child) { 78 | if (child.length() == 0 || child.equals("/")) { 79 | return parent; 80 | } 81 | 82 | if (child.charAt(0) == '/') { 83 | if (parent.equals("/")) return child; 84 | return parent + child; 85 | } 86 | 87 | if (parent.equals("/")) return parent + child; 88 | return parent + '/' + child; 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /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/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 23 | 24 | 29 | 30 |