Example usage for android.support.v4.provider DocumentFile getUri

List of usage examples for android.support.v4.provider DocumentFile getUri

Introduction

In this page you can find the example usage for android.support.v4.provider DocumentFile getUri.

Prototype

public abstract Uri getUri();

Source Link

Usage

From source file:org.totschnig.myexpenses.activity.BackupRestoreActivity.java

public static String[] listBackups() {
    DocumentFile appDir = Utils.getAppDir();
    if (appDir.getUri().getScheme().equals("file")) {
        String[] files = new File(appDir.getUri().getPath()).list(new FilenameFilter() {
            @Override//w  ww  .j  a va2  s . c o m
            public boolean accept(File dir, String filename) {
                // backup-yyyMMdd-HHmmss
                return filename.matches("backup-\\d\\d\\d\\d\\d\\d\\d\\d-\\d\\d\\d\\d\\d\\d")
                        || filename.endsWith(".zip");
            }
        });
        if (files != null) {
            Arrays.sort(files, new Comparator<String>() {
                @Override
                public int compare(String s1, String s2) {
                    return s2.compareTo(s1);
                }
            });
            return files;
        }
    }
    return new String[0];
}

From source file:com.osama.cryptofm.utils.FileDocumentUtils.java

public static DocumentFile getDocumentFile(final File file) {

    String baseFolder = SharedData.EXTERNAL_SDCARD_ROOT_PATH;
    boolean isDirectory = file.isDirectory();

    String relativePath = null;/*from ww  w  .  j  av a  2s  .  co m*/
    try {
        Log.d(TAG, "getDocumentFile: basefolder:file" + baseFolder + " : " + file.getAbsolutePath());
        String fullPath = file.getCanonicalPath();
        if ((file.getAbsolutePath() + "/").equals(baseFolder)) {
            Log.d(TAG, "getDocumentFile: uri is: " + SharedData.EXT_ROOT_URI);
            return DocumentFile.fromTreeUri(CryptoFM.getContext(), Uri.parse(SharedData.EXT_ROOT_URI));
        } else {

            relativePath = fullPath.substring(baseFolder.length());
        }
    } catch (IOException e) {
        return null;
    }

    Uri treeUri = Uri.parse(SharedData.EXT_ROOT_URI);

    if (treeUri == null) {
        return null;
    }
    relativePath = relativePath.replace(SharedData.EXTERNAL_SDCARD_ROOT_PATH, "");
    // start with root of SD card and then parse through document tree.
    Log.d(TAG, "getDocumentFile: relative path is: " + relativePath);
    DocumentFile document = DocumentFile.fromTreeUri(CryptoFM.getContext(), treeUri);

    String[] parts = relativePath.split("\\/");
    for (int i = 0; i < parts.length; i++) {
        DocumentFile nextDocument = document.findFile(parts[i]);
        Log.d(TAG, "getDocumentFile: next document is:  " + parts[i]);
        if (nextDocument == null) {
            if ((i < parts.length - 1) || isDirectory) {
                Log.d(TAG, "getDocumentFile: creating next directory");
                nextDocument = document.createDirectory(parts[i]);
            } else {
                nextDocument = document.createFile("image", parts[i]);
            }
        }
        document = nextDocument;
    }
    Log.d(TAG, "getDocumentFile: file uri is: " + document.getUri());
    return document;
}

From source file:com.almalence.util.Util.java

public static File getFileFromDocumentFile(DocumentFile documentFile) {
    try {/*from  w  w w  . j  av  a2s  . com*/
        File file = new File(URI.create(documentFile.getUri().toString()));
        return file;
    } catch (Exception e) {
        return null;
    }
}

From source file:com.frostwire.android.LollipopFileSystem.java

private static OutputStream openOutputStream(Context context, DocumentFile f) throws IOException {
    ContentResolver cr = context.getContentResolver();
    ParcelFileDescriptor pfd = cr.openFileDescriptor(f.getUri(), "rw");

    int fd = pfd.detachFd(); // this trick the internal system to trigger the media scanner on nothing
    pfd = ParcelFileDescriptor.adoptFd(fd);

    return new AutoSyncOutputStream(pfd);
}

From source file:org.dkf.jed2k.android.LollipopFileSystem.java

private static boolean copy(Context context, DocumentFile source, DocumentFile target) {
    InputStream inStream = null;// www.j  a  v  a  2 s . c o m
    OutputStream outStream = null;
    try {
        inStream = context.getContentResolver().openInputStream(source.getUri());
        outStream = openOutputStream(context, target);

        byte[] buffer = new byte[16384]; // MAGIC_NUMBER
        int bytesRead;
        while ((bytesRead = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, bytesRead);
        }

    } catch (Exception e) {
        LOG.error("Error when copying file from {} to {} {}", source.getUri(), target.getUri(), e);
        return false;
    } finally {
        IOUtils.closeQuietly(inStream);
        IOUtils.closeQuietly(outStream);
    }

    return true;
}

From source file:com.frostwire.android.LollipopFileSystem.java

private static boolean copy(Context context, DocumentFile source, DocumentFile target) {
    InputStream inStream = null;/* w  w  w . j  a  v  a  2 s.c o m*/
    OutputStream outStream = null;
    try {
        inStream = context.getContentResolver().openInputStream(source.getUri());
        outStream = openOutputStream(context, target);

        byte[] buffer = new byte[16384]; // MAGIC_NUMBER
        int bytesRead;
        while ((bytesRead = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, bytesRead);
        }

    } catch (Throwable e) {
        LOG.error("Error when copying file from " + source.getUri() + " to " + target.getUri(), e);
        return false;
    } finally {
        IOUtils.closeQuietly(inStream);
        IOUtils.closeQuietly(outStream);
    }

    return true;
}

From source file:com.almalence.util.Util.java

public static String getAbsolutePathFromDocumentFile(DocumentFile documentFile) {
    // We can't get absolute path from DocumentFile or Uri.
    // It is a hack to build absolute path by DocumentFile.
    // May not work on some devices.
    Uri uri = documentFile.getUri();
    final String docId = DocumentsContract.getDocumentId(uri);
    final String[] split = docId.split(":");
    final String id = split[1];

    String sd = null;/*from  ww w.  java2  s.  co m*/
    sd = System.getenv("SECONDARY_STORAGE");
    if (sd == null) {
        sd = System.getenv("EXTERNAL_STORAGE");
    }

    if (sd != null) {
        // On some devices SECONDARY_STORAGE has several paths
        // separated with a colon (":"). This is why we split
        // the String.
        String[] paths = sd.split(":");
        for (String p : paths) {
            File fileSD = new File(p);
            if (fileSD.isDirectory()) {
                sd = fileSD.getAbsolutePath();
            }
        }

        String documentPath = sd + "/" + id;
        return documentPath;
    }
    return null;
}

From source file:com.filemanager.free.filesystem.FileUtil.java

public static OutputStream getOutputStream(@NonNull final File target, Context context, long s)
        throws Exception {

    OutputStream outStream = null;
    try {//from ww w.  jav  a2  s. c om
        // First try the normal way
        if (isWritable(target)) {
            // standard way
            outStream = new FileOutputStream(target);
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                // Storage Access Framework
                DocumentFile targetDocument = getDocumentFile(target, false, context);
                outStream = context.getContentResolver().openOutputStream(targetDocument.getUri());
            } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
                // Workaround for Kitkat ext SD card
                return MediaStoreHack.getOutputStream(context, target.getPath());
            }

        }
    } catch (Exception e) {
        Log.e("AmazeFileUtils", "Error when copying file from " + target.getAbsolutePath(), e);
    }
    return outStream;
}

From source file:com.filemanager.free.filesystem.FileUtil.java

/**
 * Copy a file. The target file may even be on external SD card for Kitkat.
 *
 * @param source The source file//  w  ww  .  j a  v  a 2s.c  om
 * @param target The target file
 * @return true if the copying was successful.
 */
@SuppressWarnings("null")
public static boolean copyFile(final File source, final File target, Context context) {
    FileInputStream inStream = null;
    OutputStream outStream = null;
    FileChannel inChannel = null;
    FileChannel outChannel = null;
    try {
        inStream = new FileInputStream(source);

        // First try the normal way
        if (isWritable(target)) {
            // standard way
            outStream = new FileOutputStream(target);
            inChannel = inStream.getChannel();
            outChannel = ((FileOutputStream) outStream).getChannel();
            inChannel.transferTo(0, inChannel.size(), outChannel);
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                // Storage Access Framework
                DocumentFile targetDocument = getDocumentFile(target, false, context);
                outStream = context.getContentResolver().openOutputStream(targetDocument.getUri());
            } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
                // Workaround for Kitkat ext SD card
                Uri uri = MediaStoreHack.getUriFromFile(target.getAbsolutePath(), context);
                outStream = context.getContentResolver().openOutputStream(uri);
            } else {
                return false;
            }

            if (outStream != null) {
                // Both for SAF and for Kitkat, write to output stream.
                byte[] buffer = new byte[16384]; // MAGIC_NUMBER
                int bytesRead;
                while ((bytesRead = inStream.read(buffer)) != -1) {
                    outStream.write(buffer, 0, bytesRead);
                }
            }

        }
    } catch (Exception e) {
        Log.e("AmazeFileUtils",
                "Error when copying file from " + source.getAbsolutePath() + " to " + target.getAbsolutePath(),
                e);
        return false;
    } finally {
        try {
            inStream.close();
        } catch (Exception e) {
            // ignore exception
        }
        try {
            outStream.close();
        } catch (Exception e) {
            // ignore exception
        }
        try {
            inChannel.close();
        } catch (Exception e) {
            // ignore exception
        }
        try {
            outChannel.close();
        } catch (Exception e) {
            // ignore exception
        }
    }
    return true;
}

From source file:com.callrecorder.android.RecordingsAdapter.java

private void startPlayExternal(String fileName) {
    DocumentFile file = FileHelper.getStorageFile(context).findFile(fileName);
    Uri uri = FileHelper.getContentUri(context, file.getUri());

    context.startActivity(new Intent().setAction(Intent.ACTION_VIEW).setData(uri)
            .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION).setType("audio/3gpp"));
}