Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save serhiitereshchenko/cc650746c7390af9894bfc37402cf367 to your computer and use it in GitHub Desktop.
Save serhiitereshchenko/cc650746c7390af9894bfc37402cf367 to your computer and use it in GitHub Desktop.
public class StorageUriBuilder {
public static final String LOG_TAG = StorageUriBuilder.class.getName();
public static ExternalDirUri fromTree(Context context, Uri uri) {
if (BuildConfig.VERSION_CODE > Build.VERSION_CODES.KITKAT) {
return new ExternalDirUri(DocumentFile.fromTreeUri(context, uri));
} else {
return new ExternalDirUri(DocumentFile.fromFile(new File(uri.getPath())));
}
}
public static ExternalFileUri fromFileUri(Context context, Uri uri) {
if (BuildConfig.VERSION_CODE > Build.VERSION_CODES.KITKAT) {
return fromFile(DocumentFile.fromSingleUri(context, uri));
} else {
return fromFile(DocumentFile.fromFile(new File(uri.getPath())));
}
}
public static ExternalDirUri fromDir(DocumentFile dir) {
return new ExternalDirUri(dir);
}
public static ExternalFileUri fromFile(DocumentFile file) {
return new ExternalFileUri(file);
}
public static ExternalDirUri fromDirUri(Context context, Uri uri) {
return fromDir(DocumentFile.fromTreeUri(context, uri));
}
public abstract static class ExternalUri {
protected DocumentFile mFile;
private ExternalUri(DocumentFile rootTree) {
mFile = rootTree;
}
public DocumentFile getFile() {
return mFile;
}
public boolean delete() {
return mFile.delete();
}
public abstract boolean isFile();
public abstract boolean isDirectory();
public ExternalDirUri buildParent(Context context) {
DocumentFile parent;
if (BuildConfig.VERSION_CODE > Build.VERSION_CODES.KITKAT) {
parent = DocumentFile.fromTreeUri(context, mFile.getUri());
String[] segments = mFile.getUri().toString()
.replace(parent.getUri().toString(), "")
.split("%2F");
ExternalDirUri dir = fromDir(parent);
for (int i = 0; i < segments.length - 1; i++) {
dir = dir.appendDirectory(segments[i]);
}
return dir;
} else {
File file = new File(mFile.getUri().getPath());
return new ExternalDirUri(DocumentFile.fromFile(file.getParentFile()));
}
}
}
public static class ExternalDirUri extends ExternalUri {
private ExternalDirUri(DocumentFile rootTree) {
super(rootTree);
}
@Override
public boolean isFile() {
return false;
}
@Override
public boolean isDirectory() {
return true;
}
public ExternalDirUri appendDirectory(String name) {
DocumentFile dir = mFile.findFile(name);
if (dir == null || dir.isFile()) {
dir = mFile.createDirectory(name);
}
return new ExternalDirUri(dir);
}
public ExternalFileUri appendFile(String name) {
DocumentFile file = mFile.findFile(name);
if (file == null || file.isDirectory()) {
file = mFile.createFile(getMimeType(name), name);
}
return new ExternalFileUri(file);
}
private String getMimeType(String name) {
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(name);
if (extension != null) {
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
return type;
}
}
public static class ExternalFileUri extends ExternalUri {
private ExternalFileUri(DocumentFile file) {
super(file);
}
@Override
public boolean isFile() {
return true;
}
@Override
public boolean isDirectory() {
return false;
}
public InputStream getInputStream(Context context) throws FileNotFoundException {
return context.getContentResolver().openInputStream(mFile.getUri());
}
public OutputStream getOutputStream(Context context) throws FileNotFoundException {
return context.getContentResolver().openOutputStream(mFile.getUri());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment