Last active
January 15, 2017 16:37
-
-
Save serhiitereshchenko/bb0f1f46bf68f8e27f4bf30fd7297dff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ua.player.musicmallce.utils; | |
import android.content.Context; | |
import android.net.Uri; | |
import android.os.Build; | |
import android.support.v4.provider.DocumentFile; | |
import android.webkit.MimeTypeMap; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
public class StorageUriBuilder { | |
public static ExternalDirUri fromTree(Context context, Uri uri) { | |
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) { | |
return new ExternalDirUri(DocumentFile.fromTreeUri(context, uri)); | |
} else { | |
return new ExternalDirUri(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 ExternalFileUri fromFileUri(Context context, Uri uri) { | |
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) { | |
return fromFile(DocumentFile.fromSingleUri(context, uri)); | |
} else { | |
return fromFile(DocumentFile.fromFile(new File(uri.getPath()))); | |
} | |
} | |
public static ExternalDirUri fromDirUri(Context context, Uri uri) { | |
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) { | |
return fromDir(DocumentFile.fromTreeUri(context, uri)); | |
} else { | |
return fromDir(DocumentFile.fromFile(new File(uri.getPath()))); | |
} | |
} | |
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; | |
//С учетом того, что файл выбран через Intent.ACTION_OPEN_DOCUMENT_TREE | |
if (Build.VERSION.SDK_INT > 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()) { | |
String title; | |
if (name.lastIndexOf(".") > -1) { | |
title = name.substring(0, name.lastIndexOf(".")); | |
} else { | |
title = name; | |
} | |
file = mFile.createFile(getMimeType(name), title); | |
} | |
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