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

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

Introduction

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

Prototype

public DocumentFile findFile(String str) 

Source Link

Usage

From source file:org.totschnig.myexpenses.util.Utils.java

public static DocumentFile newFile(DocumentFile parentDir, String base, String mimeType, boolean addExtension) {
    int postfix = 0;
    do {/*from   w  ww  .j ava 2 s.c o  m*/
        String name = base;
        if (postfix > 0) {
            name += "_" + postfix;
        }
        if (addExtension) {
            name += "." + mimeType.split("/")[1];
        }
        if (parentDir.findFile(name) == null) {
            DocumentFile result = null;
            try {
                result = parentDir.createFile(mimeType, name);
                if (result == null) {
                    AcraHelper.report(new Exception(
                            String.format("createFile returned null: mimeType %s; name %s; parent %s", mimeType,
                                    name, parentDir.getUri().toString())));
                }
            } catch (SecurityException e) {
                AcraHelper.report(new Exception(
                        String.format("createFile threw SecurityException: mimeType %s; name %s; parent %s",
                                mimeType, name, parentDir.getUri().toString())));
            }
            return result;
        }
        postfix++;
    } while (true);
}

From source file:com.almalence.opencam.SavingService.java

@TargetApi(19)
public static DocumentFile getSaveDirNew(boolean forceSaveToInternalMemory) {
    DocumentFile saveDir = null;
    boolean usePhoneMem = true;

    String abcDir = "Camera";
    if (sortByData) {
        Calendar rightNow = Calendar.getInstance();
        abcDir = String.format("%tF", rightNow);
    }/*w  w  w.j a va2s  . co  m*/

    int saveToValue = Integer.parseInt(saveToPreference);
    if (saveToValue == 1 || saveToValue == 2) {
        boolean canWrite = false;
        String uri = saveToPath;
        try {
            saveDir = DocumentFile.fromTreeUri(ApplicationScreen.instance, Uri.parse(uri));
        } catch (Exception e) {
            saveDir = null;
        }
        List<UriPermission> perms = ApplicationScreen.instance.getContentResolver()
                .getPersistedUriPermissions();
        for (UriPermission p : perms) {
            if (p.getUri().toString().equals(uri.toString()) && p.isWritePermission()) {
                canWrite = true;
                break;
            }
        }

        if (saveDir != null && canWrite && saveDir.exists()) {
            if (sortByData) {
                DocumentFile dateFolder = saveDir.findFile(abcDir);
                if (dateFolder == null) {
                    dateFolder = saveDir.createDirectory(abcDir);
                }
                saveDir = dateFolder;
            }
            usePhoneMem = false;
        }
    }

    if (usePhoneMem || forceSaveToInternalMemory) // phone memory (internal
    // sd card)
    {
        saveDir = DocumentFile
                .fromFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM));
        DocumentFile abcFolder = saveDir.findFile(abcDir);
        if (abcFolder == null || !abcFolder.exists()) {
            abcFolder = saveDir.createDirectory(abcDir);
        }
        saveDir = abcFolder;
    }

    return saveDir;
}