use of android.support.v4.provider.DocumentFile in project Camera-Roll-Android-App by kollerlukas.
the class Copy method copyFileOntoRemovableStorage.
// for files on removable storage
static boolean copyFileOntoRemovableStorage(Context context, Uri treeUri, String path, String destination) throws IOException {
String mimeType = MediaType.getMimeType(path);
DocumentFile file = DocumentFile.fromFile(new File(destination));
if (file.exists()) {
int index = destination.lastIndexOf(".");
destination = destination.substring(0, index) + " Copy" + destination.substring(index, destination.length());
}
DocumentFile destinationFile = StorageUtil.createDocumentFile(context, treeUri, destination, mimeType);
if (destinationFile != null) {
ContentResolver resolver = context.getContentResolver();
OutputStream outputStream = resolver.openOutputStream(destinationFile.getUri());
InputStream inputStream = new FileInputStream(path);
return writeStream(inputStream, outputStream);
}
return false;
}
use of android.support.v4.provider.DocumentFile in project Camera-Roll-Android-App by kollerlukas.
the class Rename method renameFileRemovableStorage.
private boolean renameFileRemovableStorage(Context context, Uri treeUri, String path, String newFileName) {
// keep old paths to remove them from MediaStore afterwards
ArrayList<String> oldPaths = FileOperation.Util.getAllChildPaths(new ArrayList<String>(), path);
newFilePath = getNewFilePath(path, newFileName);
boolean success = false;
DocumentFile file = StorageUtil.parseDocumentFile(context, treeUri, new File(path));
if (file != null) {
success = file.renameTo(new File(newFilePath).getName());
}
// re-scan all paths
ArrayList<String> newPaths = FileOperation.Util.getAllChildPaths(new ArrayList<String>(), newFilePath);
addPathsToScan(oldPaths);
addPathsToScan(newPaths);
return success;
}
use of android.support.v4.provider.DocumentFile in project Camera-Roll-Android-App by kollerlukas.
the class StorageUtil method parseDocumentFile.
public static DocumentFile parseDocumentFile(Context context, Uri treeUri, File file) {
DocumentFile treeRoot;
try {
treeRoot = DocumentFile.fromTreeUri(context, treeUri);
} catch (IllegalArgumentException e) {
e.printStackTrace();
return null;
}
String path;
try {
path = file.getCanonicalPath();
String sdCardPath = getSdCardRootPath(context, path);
if (sdCardPath != null) {
if (sdCardPath.equals(path)) {
return treeRoot;
}
path = path.substring(sdCardPath.length() + 1);
} else {
return null;
}
} catch (IOException e) {
e.printStackTrace();
return null;
}
Log.d("StorageUtil", "path: " + path);
if (treeRoot != null) {
treeRoot = DocumentFile.fromTreeUri(context, treeUri);
String[] pathParts = path.split("/");
DocumentFile documentFile = treeRoot;
for (int i = 0; i < pathParts.length; i++) {
if (documentFile != null) {
documentFile = documentFile.findFile(pathParts[i]);
} else {
return null;
}
}
return documentFile;
}
return null;
}
use of android.support.v4.provider.DocumentFile in project Camera-Roll-Android-App by kollerlukas.
the class StorageUtil method createDocumentFile.
public static DocumentFile createDocumentFile(Context context, Uri treeUri, String path, String mimeType) {
int index = path.lastIndexOf("/");
String dirPath = path.substring(0, index);
DocumentFile file = parseDocumentFile(context, treeUri, new File(dirPath));
if (file != null) {
String name = path.substring(index + 1);
file = file.createFile(mimeType, name);
}
return file;
}
use of android.support.v4.provider.DocumentFile in project memetastic by gsantner.
the class ShareUtil method writeFile.
@SuppressWarnings({ "ResultOfMethodCallIgnored", "StatementWithEmptyBody" })
public void writeFile(final File file, final boolean isDirectory, final Callback.a2<Boolean, FileOutputStream> writeFileCallback) {
try {
FileOutputStream fileOutputStream = null;
ParcelFileDescriptor pfd = null;
final boolean existingEmptyFile = file.canWrite() && file.length() < MIN_OVERWRITE_LENGTH;
final boolean nonExistingCreatableFile = !file.exists() && file.getParentFile().canWrite();
if (existingEmptyFile || nonExistingCreatableFile) {
if (isDirectory) {
file.mkdirs();
} else {
fileOutputStream = new FileOutputStream(file);
}
} else {
DocumentFile dof = getDocumentFile(file, isDirectory);
if (dof != null && dof.getUri() != null && dof.canWrite()) {
if (isDirectory) {
// Nothing to do
} else {
pfd = _context.getContentResolver().openFileDescriptor(dof.getUri(), "rwt");
fileOutputStream = new FileOutputStream(pfd.getFileDescriptor());
}
}
}
if (writeFileCallback != null) {
writeFileCallback.callback(fileOutputStream != null || (isDirectory && file.exists()), fileOutputStream);
}
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (Exception ignored) {
}
}
if (pfd != null) {
pfd.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
Aggregations