Example usage for android.content Intent FLAG_GRANT_READ_URI_PERMISSION

List of usage examples for android.content Intent FLAG_GRANT_READ_URI_PERMISSION

Introduction

In this page you can find the example usage for android.content Intent FLAG_GRANT_READ_URI_PERMISSION.

Prototype

int FLAG_GRANT_READ_URI_PERMISSION

To view the source code for android.content Intent FLAG_GRANT_READ_URI_PERMISSION.

Click Source Link

Document

If set, the recipient of this Intent will be granted permission to perform read operations on the URI in the Intent's data and any URIs specified in its ClipData.

Usage

From source file:android.support.tests.GrantActivity.java

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 12 && resultCode == RESULT_OK) {
        final ContentResolver resolver = getContentResolver();
        resolver.takePersistableUriPermission(data.getData(),
                Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    }/*from   ww w.  j ava  2 s . co  m*/
}

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 . j a v a  2 s.co  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.jak_linux.dns66.ItemActivity.java

public void performFileSearch() {
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);

    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("*/*");
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);

    startActivityForResult(intent, READ_REQUEST_CODE);
}

From source file:com.commonsware.android.cp.v4file.FilesCPDemo.java

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    File f = new File(getFilesDir(), "test.pdf");

    if (!f.exists()) {
        AssetManager assets = getResources().getAssets();

        try {/*w  w w. jav a  2  s.c  om*/
            copy(assets.open("test.pdf"), f);
        } catch (IOException e) {
            Log.e("FileProvider", "Exception copying from assets", e);
        }
    }

    Intent i = new Intent(Intent.ACTION_VIEW, FileProvider.getUriForFile(this, AUTHORITY, f));

    i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(i);
    finish();
}

From source file:io.github.protino.codewatch.utils.FileProviderUtils.java

public static void shareBitmap(Context context, Bitmap bitmap) throws IOException {
    final String imageName = "/" + String.valueOf(System.currentTimeMillis());

    //First save the bitmap to cache directory
    File directory = new File(context.getCacheDir(), CACHE_DIR_NAME);
    if (!directory.mkdirs()) {
        //delete all data under this folder
        deleteRecursive(directory);/* w w w. j a  va 2 s. c om*/
        //recreate the directory
        directory = new File(context.getCacheDir(), CACHE_DIR_NAME);
        directory.mkdirs();
    }
    FileOutputStream stream = new FileOutputStream(directory + imageName + IMAGE_EXTENSION);
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    stream.flush();
    stream.close();

    //Now create uri through FileProvider
    File newFile = new File(directory, imageName + IMAGE_EXTENSION);
    Uri uri = FileProvider.getUriForFile(context, FILE_PROVIDER_AUTHORITY, newFile);

    if (uri != null) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.setDataAndType(uri, context.getContentResolver().getType(uri));
        intent.setType("image/png");
        intent.putExtra(Intent.EXTRA_STREAM, uri);
        context.startActivity(Intent.createChooser(intent, context.getString(R.string.choose_app)));
    }
}

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

public static String handle(Context context, int requestCode, int resultCode, Intent data) {
    String result = null;//w w  w .jav a2  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 {
                    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 w  w.  j a  va  2 s . co  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:com.pddstudio.share.Share.java

private void checkForAttachements(Intent intent) {
    if (fileAttached && fileUri != null) {
        intent.setData(fileUri);//from ww w.  j  a  va  2s.  com
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    }
}

From source file:com.anthonymandra.support.v4.provider.DocumentsContractApi19.java

public static boolean canRead(Context context, Uri self) {
    // Ignore if grant doesn't allow read
    if (context.checkCallingOrSelfUriPermission(self,
            Intent.FLAG_GRANT_READ_URI_PERMISSION) != PackageManager.PERMISSION_GRANTED) {
        return false;
    }//from w w w .  j  a  v a  2 s. com

    // Ignore documents without MIME
    if (TextUtils.isEmpty(getRawType(context, self))) {
        return false;
    }

    return true;
}

From source file:de.baumann.quitsmoking.helper.helper_main.java

public static void openFile(Activity activity, File file, String string, View view) {

    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        Uri contentUri = FileProvider.getUriForFile(activity,
                activity.getApplicationContext().getPackageName() + ".provider", file);
        intent.setDataAndType(contentUri, string);

    } else {/*from w  w  w .  ja  v a  2s  .  c  om*/
        intent.setDataAndType(Uri.fromFile(file), string);
    }

    try {
        activity.startActivity(intent);
    } catch (ActivityNotFoundException e) {
        Snackbar.make(view, R.string.toast_install_app, Snackbar.LENGTH_LONG).show();
    }
}