Search in sources :

Example 11 with DocumentFile

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;
}
Also used : DocumentFile(android.support.provider.DocumentFile) BufferedInputStream(java.io.BufferedInputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

DocumentFile (android.support.provider.DocumentFile)11 File (java.io.File)6 FileUtils.getTypeForFile (dev.dworks.apps.anexplorer.misc.FileUtils.getTypeForFile)5 DocumentInfo.getCursorString (dev.dworks.apps.anexplorer.model.DocumentInfo.getCursorString)5 UsbFile (com.github.mjdev.libaums.fs.UsbFile)3 Uri (android.net.Uri)2 UsbFileOutputStream (com.github.mjdev.libaums.fs.UsbFileOutputStream)2 IOException (java.io.IOException)2 SuppressLint (android.annotation.SuppressLint)1 Point (android.graphics.Point)1 BasicDocumentFile (android.support.provider.BasicDocumentFile)1 UsbDocumentFile (android.support.provider.UsbDocumentFile)1 FileSystem (com.github.mjdev.libaums.fs.FileSystem)1 UsbFileInputStream (com.github.mjdev.libaums.fs.UsbFileInputStream)1 RowBuilder (dev.dworks.apps.anexplorer.cursor.MatrixCursor.RowBuilder)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1