use of android.support.provider.DocumentFile in project AnExplorer by 1hakr.
the class FileUtils method copyDocument.
public static boolean copyDocument(Context context, DocumentFile file, DocumentFile dest) {
if (!file.exists() || file.isDirectory()) {
Log.v(TAG, "copyDocument: file not exist or is directory, " + file);
return false;
}
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
byte[] data = new byte[BUFFER];
int read = 0;
try {
if (!dest.exists()) {
dest = dest.getParentFile().createDirectory(dest.getName());
if (!dest.exists()) {
return false;
}
}
String mimeType = getTypeForFile(file);
String displayName = FileUtils.getNameFromFilename(file.getName());
DocumentFile destFile = dest.createFile(mimeType, displayName);
int n = 0;
while (destFile == null && n++ < 32) {
String destName = displayName + " (" + n + ")";
destFile = dest.createFile(mimeType, destName);
}
if (destFile == null) {
return false;
}
bos = new BufferedOutputStream(getOutputStream(context, destFile));
bis = new BufferedInputStream(getInputStream(context, file));
while ((read = bis.read(data, 0, BUFFER)) != -1) {
bos.write(data, 0, read);
}
return true;
} catch (FileNotFoundException e) {
Log.e(TAG, "copyDocument: file not found, " + file);
e.printStackTrace();
} catch (IOException e) {
Log.e(TAG, "copyDocument: " + e.toString());
} finally {
// flush and close
IoUtils.flushQuietly(bos);
IoUtils.closeQuietly(bos);
IoUtils.closeQuietly(bis);
}
return false;
}
Aggregations