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

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

Introduction

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

Prototype

public static DocumentFile fromTreeUri(Context context, Uri uri) 

Source Link

Usage

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;/*w  w  w  . j  av a 2  s  .c om*/
    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.frostwire.android.StoragePicker.java

public static String handle(Context context, int requestCode, int resultCode, Intent data) {
    String result = null;/*from   w  ww .  ja  v  a  2  s . c o  m*/
    try {

        if (resultCode == Activity.RESULT_OK && requestCode == SELECT_FOLDER_REQUEST_CODE) {
            Uri treeUri = data.getData();

            ContentResolver cr = context.getContentResolver();

            final int takeFlags = data.getFlags()
                    & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            cr.takePersistableUriPermission(treeUri, takeFlags);

            if (treeUri == null) {
                UIUtils.showShortMessage(context, R.string.storage_picker_treeuri_null);
                result = null;
            } else {
                DocumentFile file = DocumentFile.fromTreeUri(context, treeUri);
                if (!file.isDirectory()) {
                    UIUtils.showShortMessage(context, R.string.storage_picker_treeuri_not_directory);
                    result = null;
                } else if (!file.canWrite()) {
                    UIUtils.showShortMessage(context, R.string.storage_picker_treeuri_cant_write);
                    result = null;
                } else {
                    LollipopFileSystem fs = (LollipopFileSystem) Platforms.fileSystem();
                    result = fs.getTreePath(treeUri);
                    if (result != null && !result.endsWith("/FrostWire")) {
                        DocumentFile f = file.findFile("FrostWire");
                        if (f == null) {
                            file.createDirectory("FrostWire");
                        }
                    }
                }
            }
        }
    } catch (Throwable e) {
        UIUtils.showShortMessage(context, R.string.storage_picker_treeuri_error);
        LOG.error("Error handling folder selection", e);
        result = null;
    }

    if (result != null) {
        ConfigurationManager.instance().setStoragePath(result);
        BTEngine.ctx.dataDir = Platforms.data();
        BTEngine.ctx.torrentsDir = Platforms.torrents();
    }

    return result;
}

From source file:org.dkf.jmule.StoragePicker.java

public static String handle(Context context, int requestCode, int resultCode, Intent data) {
    String result = null;//from   w  w  w .ja  v a 2s.c  om
    try {

        if (resultCode == Activity.RESULT_OK && requestCode == SELECT_FOLDER_REQUEST_CODE) {
            Uri treeUri = data.getData();

            ContentResolver cr = context.getContentResolver();

            Method takePersistableUriPermissionM = cr.getClass().getMethod("takePersistableUriPermission",
                    Uri.class, int.class);
            final int takeFlags = data.getFlags()
                    & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            takePersistableUriPermissionM.invoke(cr, treeUri, takeFlags);

            if (treeUri == null) {
                UIUtils.showShortMessage(context, R.string.storage_picker_treeuri_null);
                result = null;
            } else {
                DocumentFile file = DocumentFile.fromTreeUri(context, treeUri);
                if (!file.isDirectory()) {
                    UIUtils.showShortMessage(context, R.string.storage_picker_treeuri_not_directory);
                    result = null;
                } else if (!file.canWrite()) {
                    UIUtils.showShortMessage(context, R.string.storage_picker_treeuri_cant_write);
                    result = null;
                } else {
                    if (Platforms.get().saf()) {
                        LollipopFileSystem fs = (LollipopFileSystem) Platforms.fileSystem();
                        result = fs.getTreePath(treeUri);

                        // TODO - remove below code - only for testing SD card writing
                        File testFile = new File(result, "test_file.txt");
                        LOG.info("test file {}", testFile);

                        try {
                            Pair<ParcelFileDescriptor, DocumentFile> fd = fs.openFD(testFile, "rw");
                            if (fd != null && fd.first != null && fd.second != null) {
                                AndroidFileHandler ah = new AndroidFileHandler(testFile, fd.second, fd.first);
                                ByteBuffer bb = ByteBuffer.allocate(48);
                                bb.putInt(1).putInt(2).putInt(3).putInt(44).putInt(22);
                                bb.flip();
                                ah.getWriteChannel().write(bb);
                                ah.close();
                            } else {
                                LOG.error("unable to create file {}", testFile);
                            }
                        } catch (Exception e) {
                            LOG.error("unable to fill file {} error {}", testFile, e);
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        UIUtils.showShortMessage(context, R.string.storage_picker_treeuri_error);
        LOG.error("Error handling folder selection {}", e);
        result = null;
    }

    return result;
}

From source file:com.frostwire.android.gui.StoragePicker.java

public static String handle(Context context, int requestCode, int resultCode, Intent data) {
    String result = null;/* w ww . j a  va 2 s  .  c  o m*/
    try {

        if (resultCode == Activity.RESULT_OK && requestCode == SELECT_FOLDER_REQUEST_CODE) {
            Uri treeUri = data.getData();

            ContentResolver cr = context.getContentResolver();

            Method takePersistableUriPermissionM = cr.getClass().getMethod("takePersistableUriPermission",
                    Uri.class, int.class);
            final int takeFlags = data.getFlags()
                    & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            takePersistableUriPermissionM.invoke(cr, treeUri, takeFlags);

            if (treeUri == null) {
                UIUtils.showShortMessage(context, R.string.storage_picker_treeuri_null);
                result = null;
            } else {
                DocumentFile file = DocumentFile.fromTreeUri(context, treeUri);
                if (!file.isDirectory()) {
                    UIUtils.showShortMessage(context, R.string.storage_picker_treeuri_not_directory);
                    result = null;
                } else if (!file.canWrite()) {
                    UIUtils.showShortMessage(context, R.string.storage_picker_treeuri_cant_write);
                    result = null;
                } else {
                    result = getFullPathFromTreeUri(context, treeUri);
                }
            }
        }
    } catch (Throwable e) {
        UIUtils.showShortMessage(context, R.string.storage_picker_treeuri_error);
        LOG.error("Error handling folder selection", e);
        result = null;
    }

    return result;
}

From source file:freed.cam.ui.themesample.settings.childs.SettingsChildMenuSDSave.java

@Override
public void onActivityResultCallback(Uri uri) {
    DocumentFile f = DocumentFile.fromTreeUri(getContext(), uri);
    if (f.canWrite() && lastval.equals(SDModeParameter.external)) {
        fragment_activityInterface.getAppSettings().SetWriteExternal(true);
        onParameterValueChanged(SDModeParameter.external);
    } else {//from  w w w. jav a  2s.c  o m
        fragment_activityInterface.getAppSettings().SetWriteExternal(false);
        onParameterValueChanged(SDModeParameter.internal);
    }
    lastval = "";
}

From source file:io.v.android.apps.syncslides.DeckChooserFragment.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case REQUEST_CODE_IMPORT_DECK:
        if (resultCode != Activity.RESULT_OK) {
            String errorStr = data != null && data.hasExtra(DocumentsContract.EXTRA_ERROR)
                    ? data.getStringExtra(DocumentsContract.EXTRA_ERROR)
                    : "";
            toast("Error selecting deck to import " + errorStr);
            break;
        }//from ww  w.  j a  va2  s  .c  o  m
        Uri uri = data.getData();
        importDeck(DocumentFile.fromTreeUri(getContext(), uri));
        break;
    }
}

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 {/*  w w w.  j a  va2  s.  c om*/
        return DocumentFile.fromTreeUri(context, uri);
    }
}

From source file:io.v.syncslides.DeckChooserFragment.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case REQUEST_CODE_IMPORT_DECK:
        if (resultCode != Activity.RESULT_OK) {
            String errorStr = data != null && data.hasExtra(DocumentsContract.EXTRA_ERROR)
                    ? data.getStringExtra(DocumentsContract.EXTRA_ERROR)
                    : "";
            toast("Error selecting deck to import " + errorStr);
            break;
        }/* ww w .j  ava  2s.c om*/
        Uri uri = data.getData();
        DeckImporter importer = new DeckImporter(getActivity().getContentResolver(), DB.Singleton.get());
        ListenableFuture<Void> future = importer.importDeck(DocumentFile.fromTreeUri(getContext(), uri));
        Futures.addCallback(future, new FutureCallback<Void>() {
            @Override
            public void onSuccess(Void result) {
                toast("Import complete");
            }

            @Override
            public void onFailure(Throwable t) {
                toast("Import failed: " + t.getMessage());
            }
        });
        break;
    }
}

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;/*ww  w. jav  a2s  .com*/

    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:com.theelix.libreexplorer.FileManagerActivity.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == FileManager.REQUEST_GET_DOCUMENT) {
            DocumentFile documentFile = DocumentFile.fromTreeUri(this, data.getData());
            try {
                FileManager.createDestFiles(documentFile);
            } catch (IOException | FileNotCreatedException e) {
                e.printStackTrace();//  ww  w.  j  a v a2s  .  c o m
            }
        }

    }
}