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:com.murrayc.galaxyzoo.app.SubjectFragment.java

private void updateShareActionIntent() {
    /**/*from  w  ww .  j av  a  2  s .  com*/
     * Initialization and setup of the share intent is done here so that less work is left after the AsyncTask's execution
     */
    if (mShareActionProvider == null) {
        Log.error("updateShareActionIntent(): mShareActionProvider is null.");
        return;
    }

    final Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_SUBJECT, "A Galaxy Zoo image.");
    //Only useful with ACTION_CHOOSER: shareIntent.putExtra(Intent.EXTRA_TITLE, "Share the Galaxy Zoo image.");

    final String uri = Utils.getTalkUri(getZooniverseId());
    if (!TextUtils.isEmpty(uri)) {
        shareIntent.putExtra(Intent.EXTRA_TEXT, uri);
        shareIntent.setType("text/plain");
    }

    //TODO: This doesn't seem to work, maybe because the other app wouldn't have access to our
    //content provider?
    //shareIntent.putExtra(Intent.EXTRA_STREAM, mUriImageStandard);
    //shareIntent.setType("image/*");

    /**
     * if the image URI is not null, a GetImageBitmapAsyncTask is executed for it, and the returned URI is set as
     * a stream extra on the intent that will be shared, along with an explicit permission for recieving contexts to
     * read the content URI, enabling them to access the generated image.
     */
    if (mUriStandardRemote != null) {
        GetImageBitmapAsyncTask getImageBitmapAsyncTask = new GetImageBitmapAsyncTask() {
            @Override
            protected void onPostExecute(Uri uri) {
                shareIntent.setType("image/*");
                shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
                shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                mShareActionProvider.setShareIntent(shareIntent);
            }
        };
        getImageBitmapAsyncTask.execute(mUriStandardRemote);
    }

}

From source file:com.gdpi.app.UpdateManager.java

/**
 * APK/*  ww  w  .j  ava2 s  .c o m*/
 */
private void installApk() {
    /**
     * android7.0
     */
    File apkfile = new File(mSavePath, name);
    if (!apkfile.exists()) {
        return;
    }
    Intent intent = new Intent(Intent.ACTION_VIEW);
    // Activity?Activity,?
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    if (Build.VERSION.SDK_INT >= N) { //?7.0
        //?1 , ?2 Provider? ???   ?3  
        Uri apkUri = FileProvider.getUriForFile(mContext, "com.gpdi.app.fileprovider", apkfile);
        //??Uri
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
    } else {
        intent.setDataAndType(Uri.fromFile(apkfile), "application/vnd.android.package-archive");
    }
    mContext.startActivity(intent);
    android.os.Process.killProcess(android.os.Process.myPid());

}

From source file:com.afwsamples.testdpc.SetupManagementFragment.java

private void specifyLogoUri(Intent intent) {
    intent.putExtra(EXTRA_PROVISIONING_LOGO_URI, mLogoUri);
    if (mLogoUri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.setClipData(ClipData.newUri(getActivity().getContentResolver(), "", mLogoUri));
    }/*from   w  ww .j  a va 2 s  .  c  o m*/
}

From source file:com.bitants.wally.activities.ImageDetailsActivity.java

private void setImageAsWallpaperPicker(Uri path) {
    Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
    intent.setType("image/*");

    MimeTypeMap map = MimeTypeMap.getSingleton();
    String mimeType = map.getMimeTypeFromExtension("png");
    intent.setDataAndType(path, mimeType);
    intent.putExtra("mimeType", mimeType);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

    startActivity(Intent.createChooser(intent, getString(R.string.action_set_as)));
}

From source file:net.gaast.giggity.ScheduleViewActivity.java

/** Open a link object, either just through the browser or by downloading locally and using a
 * dedicated viewer./*from  ww w. j  a  v  a 2 s  . c  o  m*/
 * @param link Link object - if type is set we'll try to download and use a viewer, unless:
 * @param allowDownload is set. This also used to avoid infinite loops in case of bugs.
 */
private void openLink(final Schedule.Link link, boolean allowDownload) {
    if (link.getType() != null) {
        Fetcher f = null;
        try {
            f = new Fetcher(app, link.getUrl(), Fetcher.Source.CACHE, link.getType());
        } catch (IOException e) {
            // Failure is ~expected so don't make a fuss about it at this stage. :-)
        }
        if (f != null) {
            Uri cached = f.cacheUri();
            try {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.setDataAndType(cached, link.getType());
                intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                Log.d("ScheduleViewActivity", "Viewing content externally: " + intent);
                startActivity(intent);
            } catch (ActivityNotFoundException e) {
                new AlertDialog.Builder(ScheduleViewActivity.this).setTitle(R.string.loading_error).setMessage(
                        getString(R.string.no_viewer_error) + " " + link.getType() + ": " + e.getMessage())
                        .show();
            }
            return;
        } else if (allowDownload) {
            final LoadProgress prog = new LoadProgress(this, false);
            prog.setMessage(getResources().getString(R.string.loading_image));
            prog.setDone(new LoadProgressDoneInterface() {
                @Override
                public void done() {
                    /* Try again, avoiding inifite-looping on this download just in case. */
                    openLink(link, false);
                }
            });
            prog.show();

            Thread loader = new Thread() {
                @Override
                public void run() {
                    try {
                        Fetcher f = app.fetch(link.getUrl(), Fetcher.Source.ONLINE, link.getType());
                        f.setProgressHandler(prog.getHandler());

                        /* Just slurp the entire file into a bogus buffer, what we need is the
                           file in ExternalCacheDir */
                        byte[] buf = new byte[1024];
                        //noinspection StatementWithEmptyBody
                        while (f.getStream().read(buf) != -1) {
                        }
                        f.keep();

                        /* Will trigger the done() above back in the main thread. */
                        prog.getHandler().sendEmptyMessage(LoadProgress.DONE);
                    } catch (IOException e) {
                        e.printStackTrace();
                        prog.getHandler().sendMessage(Message.obtain(prog.getHandler(), 0, e));
                    }
                }
            };
            loader.start();
            return;
        }
    }

    /* If type was not set, or if neither of the two inner ifs were true (do we have the file,
       or, are we allowed to download it?), fall back to browser. */
    Uri uri = Uri.parse(link.getUrl());
    Intent browser = new Intent(Intent.ACTION_VIEW, uri);
    browser.addCategory(Intent.CATEGORY_BROWSABLE);
    startActivity(browser);
}

From source file:org.rm3l.ddwrt.tiles.admin.nvram.AdminNVRAMTile.java

private void setShareFile(File file) {
    if (mShareActionProvider == null) {
        return;//from   w  w  w . j  av a 2  s .com
    }

    final Uri uriForFile = FileProvider.getUriForFile(mParentFragmentActivity, "org.rm3l.fileprovider", file);

    mShareActionProvider
            .setOnShareTargetSelectedListener(new ShareActionProvider.OnShareTargetSelectedListener() {
                @Override
                public boolean onShareTargetSelected(ShareActionProvider source, Intent intent) {
                    mParentFragmentActivity.grantUriPermission(intent.getComponent().getPackageName(),
                            uriForFile, Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    return true;
                }
            });

    final Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_STREAM, uriForFile);
    if (mRouter != null) {
        sendIntent.putExtra(Intent.EXTRA_SUBJECT, String.format("NVRAM Variables dump from router '%s' (%s)",
                mRouter.getName(), mRouter.getRemoteIpAddress()));
    }
    sendIntent.setData(uriForFile);
    sendIntent.setType("text/plain");
    sendIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    setShareIntent(sendIntent);

}

From source file:org.cm.podd.report.activity.SettingActivity.java

private void captureImageFromCamera() {
    mCurrentPhotoUri = getImageUri();//from   w w  w  . ja v  a  2s  .  co  m
    Intent photoTakerIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    photoTakerIntent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, 1024 * 1024);
    photoTakerIntent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION, ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    photoTakerIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCurrentPhotoUri);
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) {
        photoTakerIntent
                .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            photoTakerIntent.setClipData(ClipData.newRawUri("", mCurrentPhotoUri));
        }
    }
    startActivityForResult(photoTakerIntent, REQ_CODE_TAKE_IMAGE);
}

From source file:org.gnucash.android.export.ExportAsyncTask.java

/**
 * Starts an intent chooser to allow the user to select an activity to receive
 * the exported files./*from   w w  w .  j  av a2  s.co  m*/
 * @param paths list of full paths of the files to send to the activity.
 */
private void shareFiles(List<String> paths) {
    Intent shareIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
    shareIntent.setType("text/xml");

    ArrayList<Uri> exportFiles = convertFilePathsToUris(paths);
    shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, exportFiles);
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

    shareIntent.putExtra(Intent.EXTRA_SUBJECT,
            mContext.getString(R.string.title_export_email, mExportParams.getExportFormat().name()));

    String defaultEmail = PreferenceManager.getDefaultSharedPreferences(mContext)
            .getString(mContext.getString(R.string.key_default_export_email), null);
    if (defaultEmail != null && defaultEmail.trim().length() > 0)
        shareIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { defaultEmail });

    SimpleDateFormat formatter = (SimpleDateFormat) SimpleDateFormat.getDateTimeInstance();
    ArrayList<CharSequence> extraText = new ArrayList<>();
    extraText.add(mContext.getString(R.string.description_export_email) + " "
            + formatter.format(new Date(System.currentTimeMillis())));
    shareIntent.putExtra(Intent.EXTRA_TEXT, extraText);

    if (mContext instanceof Activity) {
        List<ResolveInfo> activities = mContext.getPackageManager().queryIntentActivities(shareIntent, 0);
        if (activities != null && !activities.isEmpty()) {
            mContext.startActivity(Intent.createChooser(shareIntent,
                    mContext.getString(R.string.title_select_export_destination)));
        } else {
            Toast.makeText(mContext, R.string.toast_no_compatible_apps_to_receive_export, Toast.LENGTH_LONG)
                    .show();
        }
    }
}

From source file:org.kontalk.util.MediaStorage.java

@TargetApi(Build.VERSION_CODES.KITKAT)
public static void requestPersistablePermissions(Context context, Uri uri) {
    context.getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
}

From source file:com.android.nfc.beam.BeamTransferManager.java

Intent buildViewIntent() {
    if (mPaths.size() == 0)
        return null;

    Intent viewIntent = new Intent(Intent.ACTION_VIEW);

    String filePath = mPaths.get(0);
    Uri mediaUri = mMediaUris.get(filePath);
    Uri uri = mediaUri != null ? mediaUri
            : FileProvider.getUriForFile(mContext, "com.google.android.nfc.fileprovider", new File(filePath));
    viewIntent.setDataAndTypeAndNormalize(uri, mMimeTypes.get(filePath));
    viewIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK
            | Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    return viewIntent;
}