Search in sources :

Example 56 with DocumentFile

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;
}
Also used : DocumentFile(android.support.v4.provider.DocumentFile) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) DocumentFile(android.support.v4.provider.DocumentFile) FileInputStream(java.io.FileInputStream) ContentResolver(android.content.ContentResolver)

Example 57 with DocumentFile

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;
}
Also used : DocumentFile(android.support.v4.provider.DocumentFile) File(java.io.File) DocumentFile(android.support.v4.provider.DocumentFile)

Example 58 with DocumentFile

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

Example 59 with DocumentFile

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;
}
Also used : DocumentFile(android.support.v4.provider.DocumentFile) File(java.io.File) DocumentFile(android.support.v4.provider.DocumentFile)

Example 60 with DocumentFile

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();
    }
}
Also used : DocumentFile(android.support.v4.provider.DocumentFile) FileOutputStream(java.io.FileOutputStream) ParcelFileDescriptor(android.os.ParcelFileDescriptor) ActivityNotFoundException(android.content.ActivityNotFoundException) IOException(java.io.IOException)

Aggregations

DocumentFile (android.support.v4.provider.DocumentFile)66 File (java.io.File)35 Uri (android.net.Uri)24 IOException (java.io.IOException)21 ContentResolver (android.content.ContentResolver)10 FileOutputStream (java.io.FileOutputStream)10 SuppressLint (android.annotation.SuppressLint)9 InputStream (java.io.InputStream)6 ParcelFileDescriptor (android.os.ParcelFileDescriptor)5 FileInputStream (java.io.FileInputStream)5 OutputStream (java.io.OutputStream)5 ActivityNotFoundException (android.content.ActivityNotFoundException)4 FileChannel (java.nio.channels.FileChannel)4 ContentValues (android.content.ContentValues)3 SharedPreferences (android.content.SharedPreferences)3 NonNull (android.support.annotation.NonNull)3 BufferedOutputStream (java.io.BufferedOutputStream)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 MalformedURLException (java.net.MalformedURLException)3 ArrayList (java.util.ArrayList)3