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

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

Introduction

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

Prototype

public abstract boolean isFile();

Source Link

Usage

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

private static DocumentFile getFile(Context context, File file, boolean create) {
    try {//from w w w . j av  a  2 s  .  c o  m
        String path = file.getAbsolutePath();
        File parent = file.getParentFile();
        if (parent == null) {
            return DocumentFile.fromFile(file);
        }

        DocumentFile f = getDirectory(context, parent, false);
        if (f == null && create) {
            f = getDirectory(context, parent, create);
        }

        if (f != null) {
            String name = file.getName();
            DocumentFile child = f.findFile(name);
            if (child != null) {
                if (child.isFile()) {
                    f = child;
                } else {
                    f = null;
                }
            } else {
                if (create) {
                    f = f.createFile("application/octet-stream", name);
                } else {
                    f = null;
                }
            }
        }

        return f;
    } catch (Exception e) {
        LOG.error("Error getting file: {} {}", file, e);
        return null;
    }
}

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

private static DocumentFile getFile(Context context, File file, boolean create) {
    try {//  ww  w  .  j a  va 2 s. c  o m
        String path = file.getAbsolutePath();
        DocumentFile cached = CACHE.get(path);
        if (cached != null && cached.isFile()) {
            return cached;
        }

        File parent = file.getParentFile();
        if (parent == null) {
            return DocumentFile.fromFile(file);
        }

        DocumentFile f = getDirectory(context, parent, false);
        if (f == null && create) {
            f = getDirectory(context, parent, create);
        }

        if (f != null) {
            String name = file.getName();
            DocumentFile child = f.findFile(name);
            if (child != null) {
                if (child.isFile()) {
                    f = child;
                } else {
                    f = null;
                }
            } else {
                if (create) {
                    f = f.createFile("application/octet-stream", name);
                } else {
                    f = null;
                }
            }
        }

        if (f != null) {
            CACHE.put(path, f);
        }

        return f;
    } catch (Throwable e) {
        LOG.error("Error getting file: " + file, e);
        return null;
    }
}

From source file:com.xperia64.timidityae.Globals.java

public static void tryToDeleteFile(Context c, String filename) {
    filename = filename.replace("//", "/");
    if (new File(filename).exists()) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Globals.theFold != null) {
            DocumentFile df = DocumentFile.fromTreeUri(c, theFold);
            String split[] = filename.split("/");
            int i;
            for (i = 0; i < split.length; i++) {
                if (split[i].equals(df.getName())) {
                    i++;//from  w  w  w  .  j  a va 2s.  c  o  m
                    break;
                }
            }
            DocumentFile xx = df;
            while (i < split.length) {
                xx = xx.findFile(split[i++]);
                //upper.append("../");
                if (xx == null) {
                    Log.e("TimidityAE Globals", "Delete file error.");
                    break;
                }
            }
            // Why on earth is DocumentFile's delete method recursive by default?
            // Seriously. I wiped my sd card twice because of this.
            if (xx != null && xx.isFile() && !xx.isDirectory()) {
                xx.delete();
            }
        } else {
            new File(filename).delete();
        }
    }
}

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

@Override
public boolean isFile(File file) {
    if (file.isFile()) {
        return true;
    }/*from w  w  w  . j  a  v  a  2s . c o  m*/

    DocumentFile f = getDocument(app, file);

    return f != null && f.isFile();
}

From source file:com.kanedias.vanilla.audiotag.PluginService.java

/**
 * Finds needed file through Document API for SAF. It's not optimized yet - you can still gain wrong URI on
 * files such as "/a/b/c.mp3" and "/b/a/c.mp3", but I consider it complete enough to be usable.
 * @param currentDir - document file representing current dir of search
 * @param remainingPathSegments - path segments that are left to find
 * @return URI for found file. Null if nothing found.
 *//*from w  w  w  . j a  v  a 2 s.c o  m*/
@Nullable
private Uri findInDocumentTree(DocumentFile currentDir, List<String> remainingPathSegments) {
    for (DocumentFile file : currentDir.listFiles()) {
        int index = remainingPathSegments.indexOf(file.getName());
        if (index == -1) {
            continue;
        }

        if (file.isDirectory()) {
            remainingPathSegments.remove(file.getName());
            return findInDocumentTree(file, remainingPathSegments);
        }

        if (file.isFile() && index == remainingPathSegments.size() - 1) {
            // got to the last part
            return file.getUri();
        }
    }

    return null;
}

From source file:de.arcus.playmusiclib.PlayMusicManager.java

/**
 * Exports a track to the sd card//from   w ww .  j  ava 2  s .com
 * @param musicTrack The music track you want to export
 * @param uri The document tree
 * @return Returns whether the export was successful
 */
public boolean exportMusicTrack(MusicTrack musicTrack, Uri uri, String path) {

    // Check for null
    if (musicTrack == null)
        return false;

    String srcFile = musicTrack.getSourceFile();

    // Could not find the source file
    if (srcFile == null)
        return false;

    String fileTmp = getTempPath() + "/tmp.mp3";

    // Copy to temp path failed
    if (!SuperUserTools.fileCopy(srcFile, fileTmp))
        return false;

    // Encrypt the file
    if (musicTrack.isEncoded()) {
        String fileTmpCrypt = getTempPath() + "/crypt.mp3";

        // Encrypts the file
        if (trackEncrypt(musicTrack, fileTmp, fileTmpCrypt)) {
            // Remove the old tmp file
            FileTools.fileDelete(fileTmp);

            // New tmp file
            fileTmp = fileTmpCrypt;
        } else {
            Logger.getInstance().logWarning("ExportMusicTrack",
                    "Encrypting failed! Continue with decrypted file.");
        }
    }

    String dest;
    Uri copyUri = null;
    if (uri.toString().startsWith("file://")) {
        // Build the full path
        dest = uri.buildUpon().appendPath(path).build().getPath();

        String parentDirectory = new File(dest).getParent();
        FileTools.directoryCreate(parentDirectory);
    } else {
        // Complex uri (Lollipop)
        dest = getTempPath() + "/final.mp3";

        // The root
        DocumentFile document = DocumentFile.fromTreeUri(mContext, uri);

        // Creates the subdirectories
        String[] directories = path.split("\\/");
        for (int i = 0; i < directories.length - 1; i++) {
            String directoryName = directories[i];
            boolean found = false;

            // Search all sub elements
            for (DocumentFile subDocument : document.listFiles()) {
                // Directory exists
                if (subDocument.isDirectory() && subDocument.getName().equals(directoryName)) {
                    document = subDocument;
                    found = true;
                    break;
                }
            }

            if (!found) {
                // Create the directory
                document = document.createDirectory(directoryName);
            }
        }

        // Gets the filename
        String filename = directories[directories.length - 1];

        for (DocumentFile subDocument : document.listFiles()) {
            // Directory exists
            if (subDocument.isFile() && subDocument.getName().equals(filename)) {
                // Delete the file
                subDocument.delete();
                break;
            }
        }

        // Create the mp3 file
        document = document.createFile("music/mp3", filename);

        // Create the directories
        copyUri = document.getUri();
    }

    // We want to export the ID3 tags
    if (mID3Enable) {
        // Adds the meta data
        if (!trackWriteID3(musicTrack, fileTmp, dest)) {
            Logger.getInstance().logWarning("ExportMusicTrack",
                    "ID3 writer failed! Continue without ID3 tags.");

            // Failed, moving without meta data
            if (!FileTools.fileMove(fileTmp, dest)) {
                Logger.getInstance().logError("ExportMusicTrack", "Moving the raw file failed!");

                // Could not copy the file
                return false;
            }
        }
    } else {
        // Moving the file
        if (!FileTools.fileMove(fileTmp, dest)) {
            Logger.getInstance().logError("ExportMusicTrack", "Moving the raw file failed!");

            // Could not copy the file
            return false;
        }
    }

    // We need to copy the file to a uri
    if (copyUri != null) {
        // Lollipop only
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            try {
                // Gets the file descriptor
                ParcelFileDescriptor parcelFileDescriptor = mContext.getContentResolver()
                        .openFileDescriptor(copyUri, "w");

                // Gets the output stream
                FileOutputStream fileOutputStream = new FileOutputStream(
                        parcelFileDescriptor.getFileDescriptor());

                // Gets the input stream
                FileInputStream fileInputStream = new FileInputStream(dest);

                // Copy the stream
                FileTools.fileCopy(fileInputStream, fileOutputStream);

                // Close all streams
                fileOutputStream.close();
                fileInputStream.close();
                parcelFileDescriptor.close();

            } catch (FileNotFoundException e) {
                Logger.getInstance().logError("ExportMusicTrack", "File not found!");

                // Could not copy the file
                return false;
            } catch (IOException e) {
                Logger.getInstance().logError("ExportMusicTrack",
                        "Failed to write the document: " + e.toString());

                // Could not copy the file
                return false;
            }
        }
    }

    // Delete temp files
    cleanUp();

    // Adds the file to the media system
    //new MediaScanner(mContext, dest);

    // Done
    return true;
}