use of android.support.provider.DocumentFile in project AnExplorer by 1hakr.
the class UsbStorageProvider method copy.
private String copy(String sourceDocumentId, String targetParentDocumentId) throws IOException {
final String afterDocId;
final UsbFile before = getFile(sourceDocumentId);
final UsbFile after = getFile(targetParentDocumentId);
boolean isSourceUSB = sourceDocumentId.startsWith(ROOT_ID_USB);
boolean isTargetUSB = targetParentDocumentId.startsWith(ROOT_ID_USB);
if (!(isSourceUSB && isTargetUSB)) {
DocumentFile sourceDirectory = getDocumentFile(sourceDocumentId);
DocumentFile targetDirectory = getDocumentFile(targetParentDocumentId);
if (!FileUtils.moveDocument(getContext(), sourceDirectory, targetDirectory)) {
throw new IllegalStateException("Failed to copy ");
}
afterDocId = targetParentDocumentId;
} else {
UsbPartition usbPartition = mRoots.get(getRootIdForDocId(sourceDocumentId));
final FileSystem fileSystem = usbPartition.fileSystem;
final UsbFile newFile = after.createFile(before.getName());
InputStream inputStream = UsbFileStreamFactory.createBufferedInputStream(before, fileSystem);
OutputStream outputStream = UsbFileStreamFactory.createBufferedOutputStream(newFile, fileSystem);
if (!FileUtils.copy(inputStream, outputStream)) {
throw new IllegalStateException("Failed to copy " + before);
}
afterDocId = getDocIdForFile(after);
}
return afterDocId;
}
use of android.support.provider.DocumentFile in project AnExplorer by 1hakr.
the class ExternalStorageProvider method createDocument.
@Override
public String createDocument(String docId, String mimeType, String displayName) throws FileNotFoundException {
displayName = FileUtils.buildValidFatFilename(displayName);
final File parent = getFileForDocId(docId);
if (!parent.isDirectory()) {
throw new IllegalArgumentException("Parent document isn't a directory");
}
final File file = FileUtils.buildUniqueFile(parent, mimeType, displayName);
DocumentFile documentFile = getDocumentFile(docId, parent);
if (Document.MIME_TYPE_DIR.equals(mimeType)) {
DocumentFile newFile = documentFile.createDirectory(displayName);
if (!newFile.exists()) {
throw new IllegalStateException("Failed to mkdir " + file);
}
} else {
DocumentFile newFile = documentFile.createFile(mimeType, displayName);
if (!newFile.exists()) {
throw new IllegalStateException("Failed to touch " + file);
}
}
final String afterDocId = getDocIdForFile(file);
notifyDocumentsChanged(afterDocId);
return afterDocId;
}
use of android.support.provider.DocumentFile in project AnExplorer by 1hakr.
the class ExternalStorageProvider method move.
private String move(String sourceDocumentId, String targetParentDocumentId) throws FileNotFoundException {
final String afterDocId;
final File source = getFile(sourceDocumentId);
final File target = getFile(targetParentDocumentId);
boolean isSourceOther = isFromOtherProvider(sourceDocumentId);
boolean isTargetOther = isFromOtherProvider(targetParentDocumentId);
if ((isSourceOther || isTargetOther)) {
DocumentFile sourceDirectory = getDocumentFile(sourceDocumentId, source);
DocumentFile targetDirectory = getDocumentFile(targetParentDocumentId, target);
if (!FileUtils.moveDocument(getContext(), sourceDirectory, targetDirectory)) {
throw new IllegalStateException("Failed to move " + source);
} else {
if (!sourceDirectory.delete()) {
throw new IllegalStateException("Failed to move " + source);
}
}
afterDocId = targetParentDocumentId;
} else {
final File after = new File(target, source.getName());
if (after.exists()) {
throw new IllegalStateException("Already exists " + after);
}
if (!source.renameTo(after)) {
throw new IllegalStateException("Failed to move to " + after);
} else {
notifyDocumentsChanged(targetParentDocumentId);
FileUtils.updateMediaStore(getContext(), source.getPath());
}
afterDocId = getDocIdForFile(target);
}
return afterDocId;
}
use of android.support.provider.DocumentFile in project AnExplorer by 1hakr.
the class ExternalStorageProvider method copy.
private String copy(String sourceDocumentId, String targetParentDocumentId) throws FileNotFoundException {
final String afterDocId;
final File source = getFile(sourceDocumentId);
final File target = getFile(targetParentDocumentId);
boolean isSourceOther = isFromOtherProvider(sourceDocumentId);
boolean isTargetOther = isFromOtherProvider(targetParentDocumentId);
if ((isSourceOther || isTargetOther)) {
DocumentFile sourceDirectory = getDocumentFile(sourceDocumentId, source);
DocumentFile targetDirectory = getDocumentFile(targetParentDocumentId, target);
if (!FileUtils.moveDocument(getContext(), sourceDirectory, targetDirectory)) {
throw new IllegalStateException("Failed to copy " + source);
}
afterDocId = targetParentDocumentId;
} else {
if (!FileUtils.moveDocument(source, target, null)) {
throw new IllegalStateException("Failed to copy " + source);
}
afterDocId = getDocIdForFile(target);
}
return afterDocId;
}
use of android.support.provider.DocumentFile in project AnExplorer by 1hakr.
the class NoteActivity method getOutStream.
private OutputStream getOutStream(Uri uri) {
String scheme = uri.getScheme();
if (scheme.startsWith(ContentResolver.SCHEME_CONTENT)) {
try {
DocumentFile documentFile = DocumentsApplication.getSAFManager(getApplicationContext()).getDocumentFile(uri);
Uri finalUri = null == documentFile ? uri : documentFile.getUri();
return getContentResolver().openOutputStream(finalUri);
} catch (Exception e) {
CrashReportingManager.logException(e);
}
} else if (scheme.startsWith(ContentResolver.SCHEME_FILE)) {
File f = new File(uri.getPath());
if (f.exists()) {
try {
return new FileOutputStream(f);
} catch (Exception e) {
CrashReportingManager.logException(e);
}
}
}
return null;
}
Aggregations