Java Code Examples for android.support.v4.provider.DocumentFile#fromFile()

The following examples show how to use android.support.v4.provider.DocumentFile#fromFile() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Copy.java    From Camera-Roll-Android-App with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 2
Source File: FileUtil.java    From FileManager with Apache License 2.0 6 votes vote down vote up
public static boolean renameTarget(String filePath, String newName) {
    File src = new File(filePath);

    String temp = filePath.substring(0, filePath.lastIndexOf("/"));
    File dest = new File(temp + "/" + newName);

    if (src.renameTo(dest)) {
        return true;
    } else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            DocumentFile document = DocumentFile.fromFile(src);

            if (document.renameTo(dest.getAbsolutePath())) {
                return true;
            }
        }
    }
    return false;
}
 
Example 3
Source File: FileUtil.java    From FileManager with Apache License 2.0 6 votes vote down vote up
public static boolean createDir(File folder) {
    if (folder.exists())
        return false;

    if (folder.mkdir())
        return true;
    else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            DocumentFile document = DocumentFile.fromFile(folder.getParentFile());
            if (document.exists())
                return true;
        }

        if (Settings.rootAccess()) {
            return RootCommands.createRootdir(folder);
        }
    }

    return false;
}
 
Example 4
Source File: SettingsImpl.java    From ToGoZip with GNU General Public License v3.0 6 votes vote down vote up
/**
 * calculates the dafault-path value for 2go.zip
 */
public static String getDefaultZipDirPath(Context context) {
    File rootDir = null;
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        // before api-14/android-4.4/KITKAT
        // write support on sdcard, if mounted
        Boolean isSDPresent = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
        rootDir = ((isSDPresent)) ? Environment.getExternalStorageDirectory() : Environment.getRootDirectory();
    } else if (Global.USE_DOCUMENT_PROVIDER && (zipDocDirUri != null)) {

        // DocumentFile docDir = DocumentFile.fromTreeUri(context, Uri.parse(zipDocDirUri));
        DocumentFile docDir = DocumentFile.fromFile(new File(zipDocDirUri));
        if ((docDir != null) && docDir.canWrite()) {
            return rootDir.getAbsolutePath();
        }
    }

    if (rootDir == null) {
        // since android 4.4 Environment.getDataDirectory() and .getDownloadCacheDirectory ()
        // is protected by android-os :-(
        rootDir = getRootDir44();
    }

    final String zipfile = rootDir.getAbsolutePath() + "/copy";
    return zipfile;
}
 
Example 5
Source File: SettingsImpl.java    From ToGoZip with GNU General Public License v3.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static DocumentFile getDocFile(Context context, @NonNull String dir ) {
    DocumentFile docDir = null;

    if (dir.indexOf(":") >= 0) {
        Uri uri = Uri.parse(dir);

        if ("file".equals(uri.getScheme())) {
            File fileDir = new File(uri.getPath());
            docDir = DocumentFile.fromFile(fileDir);
        } else {
            docDir = DocumentFile.fromTreeUri(context, uri);
        }
    } else {
        docDir = DocumentFile.fromFile(new File(dir));
    }
    return docDir;

}
 
Example 6
Source File: SimpleUtils.java    From SimpleExplorer with GNU General Public License v3.0 6 votes vote down vote up
public static boolean renameTarget(String filePath, String newName) {
    File src = new File(filePath);

    String temp = filePath.substring(0, filePath.lastIndexOf("/"));
    File dest = new File(temp + "/" + newName);

    if (src.renameTo(dest)) {
        return true;
    } else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            DocumentFile document = DocumentFile.fromFile(src);

            if (document.renameTo(dest.getAbsolutePath())) {
                return true;
            }
        }
    }
    return false;
}
 
Example 7
Source File: SimpleUtils.java    From SimpleExplorer with GNU General Public License v3.0 6 votes vote down vote up
public static boolean createDir(File folder) {
    if (folder.exists())
        return false;

    if (folder.mkdir())
        return true;
    else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            DocumentFile document = DocumentFile.fromFile(folder.getParentFile());
            if (document.exists())
                return true;
        }

        if (Settings.rootAccess()) {
            return RootCommands.createRootdir(folder);
        }
    }

    return false;
}