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

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

Introduction

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

Prototype

public static DocumentFile fromFile(File file) 

Source Link

Usage

From source file:org.totschnig.myexpenses.test.model.ExportTest.java

@Override
protected void setUp() throws Exception {
    super.setUp();
    outDir = DocumentFile.fromFile(getContext().getCacheDir());
}

From source file:com.maskyn.fileeditorpro.dialogfragment.FileInfoDialog.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    View view = new DialogHelper.Builder(getActivity()).setTitle(R.string.info)
            .setView(R.layout.dialog_fragment_file_info).createSkeletonView();
    //final View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_fragment_file_info, null);

    ListView list = (ListView) view.findViewById(android.R.id.list);

    DocumentFile file = DocumentFile.fromFile(
            new File(AccessStorageApi.getPath(getActivity(), (Uri) getArguments().getParcelable("uri"))));

    if (file == null && Device.hasKitKatApi()) {
        file = DocumentFile.fromSingleUri(getActivity(), (Uri) getArguments().getParcelable("uri"));
    }/*from   w  w w.jav  a  2s. c  o m*/

    // Get the last modification information.
    Long lastModified = file.lastModified();

    // Create a new date object and pass last modified information
    // to the date object.
    Date date = new Date(lastModified);

    String[] lines1 = { getString(R.string.name),
            //getString(R.string.folder),
            getString(R.string.size), getString(R.string.modification_date) };
    String[] lines2 = { file.getName(),
            //file.getParent(),
            FileUtils.byteCountToDisplaySize(file.length()), date.toString() };

    list.setAdapter(new AdapterTwoItem(getActivity(), lines1, lines2));

    return new AlertDialog.Builder(getActivity()).setView(view)
            .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                }
            }).create();
}

From source file:com.commonsware.android.tte.DocumentStorageService.java

private void load(Uri document) {
    try {/*  w w  w .  j a v  a  2 s. co  m*/
        boolean weHavePermission = false;
        boolean isContent = ContentResolver.SCHEME_CONTENT.equals(document.getScheme());

        if (isContent) {
            int perms = Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION;

            getContentResolver().takePersistableUriPermission(document, perms);

            for (UriPermission perm : getContentResolver().getPersistedUriPermissions()) {
                if (perm.getUri().equals(document)) {
                    weHavePermission = true;
                }
            }
        } else {
            weHavePermission = true;
        }

        if (weHavePermission) {
            try {
                InputStream is = getContentResolver().openInputStream(document);

                try {
                    String text = slurp(is);
                    DocumentFile docFile;

                    if (isContent) {
                        docFile = DocumentFile.fromSingleUri(this, document);
                    } else {
                        docFile = DocumentFile.fromFile(new File(document.getPath()));
                    }

                    EventBus.getDefault().post(
                            new DocumentLoadedEvent(document, text, docFile.getName(), docFile.canWrite()));
                } finally {
                    is.close();
                }
            } catch (Exception e) {
                Log.e(getClass().getSimpleName(), "Exception loading " + document.toString(), e);
                EventBus.getDefault().post(new DocumentLoadErrorEvent(document, e));
            }
        } else {
            Log.e(getClass().getSimpleName(), "We failed to get permissions for " + document.toString());
            EventBus.getDefault().post(new DocumentPermissionFailureEvent(document));
        }
    } catch (SecurityException e) {
        Log.e(getClass().getSimpleName(), "Exception getting permissions for " + document.toString(), e);
        EventBus.getDefault().post(new DocumentPermissionFailureEvent(document));
    }
}

From source file:com.commonsware.android.documents.consumer.DurablizerService.java

private DocumentFile buildDocFileForUri(Uri document) {
    DocumentFile docFile;//from w  w w  . j a v a  2  s. c  o m

    if (document.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
        docFile = DocumentFile.fromSingleUri(this, document);
    } else {
        docFile = DocumentFile.fromFile(new File(document.getPath()));
    }

    return (docFile);
}

From source file:de.k3b.android.toGoZip.SettingsImpl.java

/**
 * calculates the dafault-path value for 2go.zip
 *///from ww  w .ja  va 2  s . com
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;
}

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

public static DocumentFile getStorageFile(Context context) {
    Uri uri = UserPreferences.getStorageUri();
    String scheme = uri.getScheme();
    if (scheme == null || scheme.equals("file")) {
        return DocumentFile.fromFile(new File(uri.getPath()));
    } else {//from w ww.  j  av a2 s .  c  om
        return DocumentFile.fromTreeUri(context, uri);
    }
}

From source file:de.k3b.android.toGoZip.SettingsImpl.java

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static DocumentFile getDocFile(Context context, @NonNull String dir) {
    DocumentFile docDir = null;//from   w  ww  .ja  v  a  2 s. co  m

    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;

}

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

private static DocumentFile getDirectory(Context context, File dir, boolean create) {
    try {//from   w  w  w.j  a  v  a  2 s  .  c o  m
        String path = dir.getAbsolutePath();

        String baseFolder = getExtSdCardFolder(context, dir);
        if (baseFolder == null) {
            if (create) {
                return dir.mkdirs() ? DocumentFile.fromFile(dir) : null;
            } else {
                return dir.isDirectory() ? DocumentFile.fromFile(dir) : null;
            }
        }

        baseFolder = combineRoot(baseFolder);

        String fullPath = dir.getAbsolutePath();
        String relativePath = baseFolder.length() < fullPath.length()
                ? fullPath.substring(baseFolder.length() + 1)
                : "";

        String[] segments = relativePath.isEmpty() ? new String[0] : relativePath.split("/");

        Uri rootUri = getDocumentUri(context, new File(baseFolder));
        DocumentFile f = DocumentFile.fromTreeUri(context, rootUri);

        for (int i = 0; i < segments.length; i++) {
            String segment = segments[i];
            DocumentFile child = f.findFile(segment);
            if (child != null) {
                f = child;
            } else {
                if (create) {
                    f = f.createDirectory(segment);
                    if (f == null) {
                        return null;
                    }
                } else {
                    return null;
                }
            }
        }

        return f.isDirectory() ? f : null;
    } catch (Exception e) {
        LOG.error("Error getting directory: {} {}", dir, e);
        return null;
    }
}

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

private static DocumentFile getDirectory(Context context, File dir, boolean create) {
    try {/*w w w  .jav a2 s  .co  m*/
        String path = dir.getAbsolutePath();
        DocumentFile cached = CACHE.get(path);
        if (cached != null && cached.isDirectory()) {
            return cached;
        }

        String baseFolder = getExtSdCardFolder(context, dir);
        if (baseFolder == null) {
            if (create) {
                return dir.mkdirs() ? DocumentFile.fromFile(dir) : null;
            } else {
                return dir.isDirectory() ? DocumentFile.fromFile(dir) : null;
            }
        }

        baseFolder = combineRoot(baseFolder);

        String fullPath = dir.getAbsolutePath();
        String relativePath = baseFolder.length() < fullPath.length()
                ? fullPath.substring(baseFolder.length() + 1)
                : "";

        String[] segments = relativePath.split("/");

        Uri rootUri = getDocumentUri(context, new File(baseFolder));
        DocumentFile f = DocumentFile.fromTreeUri(context, rootUri);

        // special FrostWire case
        if (create) {
            if (baseFolder.endsWith("/FrostWire") && !f.exists()) {
                baseFolder = baseFolder.substring(0, baseFolder.length() - 10);
                rootUri = getDocumentUri(context, new File(baseFolder));
                f = DocumentFile.fromTreeUri(context, rootUri);
                f = f.findFile("FrostWire");
                if (f == null) {
                    f = f.createDirectory("FrostWire");
                    if (f == null) {
                        return null;
                    }
                }
            }
        }
        for (String segment : segments) {
            DocumentFile child = f.findFile(segment);
            if (child != null) {
                f = child;
            } else {
                if (create) {
                    f = f.createDirectory(segment);
                    if (f == null) {
                        return null;
                    }
                } else {
                    return null;
                }
            }
        }

        f = f.isDirectory() ? f : null;

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

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

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

private static DocumentFile getFile(Context context, File file, boolean create) {
    try {//from  w w  w  . ja  v  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;
    }
}