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.tct.mail.browse.MessageAttachmentBar.java

@Override
public void viewAttachment() {
    if (mAttachment.contentUri == null) {
        LogUtils.e(LOG_TAG, "viewAttachment with null content uri");
        return;//from w  ww. j  a v a  2 s  .com
    }

    //TS: junwei-xu 2016-03-16 EMAIL BUGFIX-1740140 ADD_S
    if (MimeType.isXvcardType(mAttachment.getContentType())) {
        importXvcard(mAttachment, mAttachment.getContentType());
        return;
    }
    //TS: junwei-xu 2016-03-16 EMAIL BUGFIX-1740140 ADD_E
    //TS: zheng.zou 2016-04-14 EMAIL BUGFIX-1779964 MOD_S
    //NOTE:Here for no screen flash when view image with Gallery,gallery call us must remove
    //any Intent TASK TAG...(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET)
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    //TS: zheng.zou 2016-04-14 EMAIL BUGFIX-1779964 MOD_E
    final String contentType = mAttachment.getContentType();
    // TS: zhaotianyong 2014-11-25 EMAIL BUGFIX-847926 ADD_S
    String str = mAttachment.contentUri.toString();
    // TS: zhaotianyong 2015-01-27 EMAIL BUGFIX-912160 MOD_S
    if (str.endsWith("RAW") && mAttachment.getName() != null)
    // TS: zhaotianyong 2015-01-27 EMAIL BUGFIX-912160 MOD_E
    {
        // TS: zhaotianyong 2015-02-14 EMAIL BUGFIX-928905 MOD_S
        str = str.replace("RAW", "RAW/" + mAttachment.getName());
        // TS: zhaotianyong 2015-02-14 EMAIL BUGFIX-928905 MOD_E
    }
    // TS: zhaotianyong 2014-11-25 EMAIL BUGFIX-847926 ADD_E
    //TS: zhonghua.tuo 2015-3-3 EMAIL BUGFIX_936728 MOD_S
    //TS: xiangnan.zhou 2016-03-17 EMAIL BUGFIX_1785573 MOD_S
    //NOTE:remove NONE-Text/HTML judge,here if file exist in local,just use file:///,it means
    //use real uri
    if (fileExist()) {//TS: zhaotianyong 2015-03-13 EMAIL BUGFIX_945771 MOD
        Utils.setIntentDataAndTypeAndNormalize(intent, mAttachment.realUri, contentType);
    } else {
        Utils.setIntentDataAndTypeAndNormalize(intent, Uri.parse(str)/*mAttachment.contentUri*/, contentType);
    }
    //TS: xiangnan.zhou 2016-03-17 EMAIL BUGFIX_1785573 MOD_S
    //TS: zhonghua.tuo 2015-3-3 EMAIL BUGFIX_936728 MOD_E
    // For EML files, we want to open our dedicated
    // viewer rather than let any activity open it.
    if (MimeType.isEmlMimeType(contentType)) {
        intent.setPackage(getContext().getPackageName());
        intent.putExtra(AccountFeedbackActivity.EXTRA_ACCOUNT_URI, mAccount != null ? mAccount.uri : null);
    }

    try {
        getContext().startActivity(intent);
    } catch (ActivityNotFoundException e) {
        // couldn't find activity for View intent
        LogUtils.e(LOG_TAG, e, "Couldn't find Activity for intent");
        // TS: zhaotianyong 2015-05-19 EMAIL BUGFIX-1006010 MOD_S
        // TS: zhaotianyong 2015-05-05 EMAIL BUGFIX-989483 ADD_S
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        int dialogMessage = R.string.no_application_found;
        builder.setTitle(R.string.more_info_attachment).setMessage(dialogMessage).show();
        // TS: zhaotianyong 2015-05-05 EMAIL BUGFIX-989483 ADD_E
        // TS: zhaotianyong 2015-05-19 EMAIL BUGFIX-1006010 MOD_E
    }
}

From source file:cn.edu.wyu.documentviewer.DocumentsActivity.java

public void onDocumentPicked(DocumentInfo doc) {
    final FragmentManager fm = getFragmentManager();
    if (doc.isDirectory()) {
        if (DEBUG) {
            Log.i(TAG, "onDocumentPicked()+directory");
        }//from  ww  w.  j av  a2 s  . c om
        mState.stack.push(doc);
        mState.stackTouched = true;
        onCurrentDirectoryChanged(ANIM_DOWN);
    } else if (mState.action == ACTION_OPEN || mState.action == ACTION_GET_CONTENT) {
        // Explicit file picked, return
        if (DEBUG) {
            Log.i(TAG, "onDocumentPicked() " + mState.action);
        }
        new ExistingFinishTask(doc.derivedUri).executeOnExecutor(getCurrentExecutor());

        //           final Intent view = new Intent(Intent.ACTION_VIEW);
        //            view.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        //            view.setData(doc.derivedUri);

        //            try {
        //                startActivity(view);
        //            } catch (ActivityNotFoundException ex2) {
        //                Toast.makeText(this, R.string.toast_no_application, Toast.LENGTH_SHORT).show();
        //            }            
    } else if (mState.action == ACTION_CREATE) {
        // Replace selected file
        if (DEBUG) {
            Log.i(TAG, "onDocumentPicked() " + mState.action);
        }
        SaveFragment.get(fm).setReplaceTarget(doc);
    } else if (mState.action == ACTION_MANAGE) {
        // First try managing the document; we expect manager to filter
        // based on authority, so we don't grant.
        if (DEBUG) {
            Log.i(TAG, "onDocumentPicked() " + mState.action);
        }
        final Intent manage = new Intent(DocumentsContract.ACTION_MANAGE_DOCUMENT);
        manage.setData(doc.derivedUri);

        try {
            startActivity(manage);
        } catch (ActivityNotFoundException ex) {
            // Fall back to viewing
            final Intent view = new Intent(Intent.ACTION_VIEW);
            view.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            view.setData(doc.derivedUri);

            try {
                startActivity(view);
            } catch (ActivityNotFoundException ex2) {
                Toast.makeText(this, R.string.toast_no_application, Toast.LENGTH_SHORT).show();
            }
        }
    }
}

From source file:com.ubuntuone.android.files.activity.PreferencesActivity.java

private void reviewLogs() {
    final File logFile = getLogFile();
    final Uri uri = Uri.fromFile(logFile);
    Log.i(TAG, "reviewing " + uri.toString());
    final Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.setDataAndType(uri, "text/plain");
    try {/*from w  w w.ja v a  2  s.c  o  m*/
        startActivity(intent);
    } catch (ActivityNotFoundException e) {
        UIUtil.showToast(mContext, R.string.no_text_viewer, true);
    }
}

From source file:net.gsantner.opoc.util.ShareUtil.java

/**
 * Request edit of image (by image editor/viewer - for example to crop image)
 *
 * @param file File that should be edited
 *///from  w  w  w  .ja  va 2s . c o  m
public void requestPictureEdit(File file) {
    Uri uri = getUriByFileProviderAuthority(file);
    int flags = Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION;

    Intent intent = new Intent(Intent.ACTION_EDIT);
    intent.setDataAndType(uri, "image/*");
    intent.addFlags(flags);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
    intent.putExtra(EXTRA_FILEPATH, file.getAbsolutePath());

    for (ResolveInfo resolveInfo : _context.getPackageManager().queryIntentActivities(intent,
            PackageManager.MATCH_DEFAULT_ONLY)) {
        String packageName = resolveInfo.activityInfo.packageName;
        _context.grantUriPermission(packageName, uri, flags);
    }
    _context.startActivity(Intent.createChooser(intent, null));
}

From source file:com.android.documentsui.DocumentsActivity.java

public void onDocumentPicked(DocumentInfo doc) {
    final FragmentManager fm = getFragmentManager();
    if (doc.isDirectory()) {
        mState.stack.push(doc);/*from w w w  .  j  a  v a 2s.  c o  m*/
        mState.stackTouched = true;
        onCurrentDirectoryChanged(ANIM_DOWN);
    } else if (mState.action == ACTION_OPEN || mState.action == ACTION_GET_CONTENT) {
        // Explicit file picked, return
        new ExistingFinishTask(doc.derivedUri).executeOnExecutor(getCurrentExecutor());
    } else if (mState.action == ACTION_CREATE) {
        // Replace selected file
        SaveFragment.get(fm).setReplaceTarget(doc);
    } else if (mState.action == ACTION_MANAGE) {
        // First try managing the document; we expect manager to filter
        // based on authority, so we don't grant.
        final Intent manage = new Intent(DocumentsContract.ACTION_MANAGE_DOCUMENT);
        manage.setData(doc.derivedUri);

        try {
            startActivity(manage);
        } catch (ActivityNotFoundException ex) {
            // Fall back to viewing
            final Intent view = new Intent(Intent.ACTION_VIEW);
            view.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            view.setData(doc.derivedUri);

            try {
                startActivity(view);
            } catch (ActivityNotFoundException ex2) {
                Toast.makeText(this, R.string.toast_no_application, Toast.LENGTH_SHORT).show();
            }
        }
    }
}

From source file:com.qiscus.sdk.ui.fragment.QiscusBaseChatFragment.java

@Override
public void onFileDownloaded(File file, String mimeType) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        intent.setDataAndType(Uri.fromFile(file), mimeType);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    } else {//from   w w w . j  ava 2s .c o  m
        intent.setDataAndType(FileProvider.getUriForFile(getActivity(), Qiscus.getProviderAuthorities(), file),
                mimeType);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    }
    try {
        startActivity(intent);
    } catch (ActivityNotFoundException e) {
        showError(getString(R.string.chat_error_no_handler));
    }
}

From source file:nl.sogeti.android.gpstracker.viewer.LoggerMap.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    boolean handled = false;

    Uri trackUri;//from ww  w .j ava2s . c om
    Intent intent;
    switch (item.getItemId()) {
    case MENU_LAYERS:
        showDialog(DIALOG_LAYERS);
        handled = true;
        break;
    case MENU_NOTE:
        intent = new Intent(this, InsertNote.class);
        startActivityForResult(intent, MENU_NOTE);
        handled = true;
        break;
    case MENU_SETTINGS:
        intent = new Intent(this, SettingsActivity.class);
        startActivity(intent);
        handled = true;
        break;
    case MENU_TRACKLIST:
        intent = new Intent(this, TrackList.class);
        intent.putExtra(Tracks._ID, this.mTrackId);
        startActivityForResult(intent, MENU_TRACKLIST);
        break;
    case MENU_STATS:
        if (this.mTrackId >= 0) {
            intent = new Intent(this, Statistics.class);
            trackUri = ContentUris.withAppendedId(Tracks.CONTENT_URI, mTrackId);
            intent.setData(trackUri);
            startActivity(intent);
            handled = true;
            break;
        } else {
            showDialog(DIALOG_NOTRACK);
        }
        handled = true;
        break;
    case MENU_SHARE:
        if (this.mTrackId >= 0) {
            intent = new Intent(Intent.ACTION_RUN);
            trackUri = ContentUris.withAppendedId(Tracks.CONTENT_URI, mTrackId);
            intent.setDataAndType(trackUri, Tracks.CONTENT_ITEM_TYPE);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Bitmap bm = findViewById(R.id.mapScreen).getDrawingCache();
            if (bm != null) {
                Uri screenStreamUri = ShareTrack.storeScreenBitmap(bm);
                intent.putExtra(Intent.EXTRA_STREAM, screenStreamUri);
            }
            startActivityForResult(Intent.createChooser(intent, getString(R.string.share_track)), MENU_SHARE);
        } else {
            showDialog(DIALOG_NOTRACK);
        }
        handled = true;
        break;
    case MENU_CONTRIB:
        showDialog(DIALOG_CONTRIB);
    default:
        handled = super.onOptionsItemSelected(item);
        break;
    }
    return handled;
}

From source file:de.cachebox_test.splash.java

@TargetApi(Build.VERSION_CODES.KITKAT)
@Override/* w  ww.jav  a2  s .c  om*/
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == RESULT_OK && requestCode == Global.REQUEST_CODE_GET_WRITE_PERMISSION_ANDROID_5) {
        Uri treeUri = data.getData();

        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        final int takeFlags = intent.getFlags()
                & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

        // Check for the freshest data.

        // Uri workPathUri = Uri.parse(workPath);

        ContentResolver cr = getContentResolver();

        grantUriPermission(getPackageName(), treeUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
        cr.takePersistableUriPermission(treeUri, takeFlags);

        List<UriPermission> permissionlist = cr.getPersistedUriPermissions();

        LolipopworkPath = "content://com.android.externalstorage.documents/tree/B8C5-760B%3A";// treeUri.getPath();

        Thread th = new Thread(new Runnable() {

            @Override
            public void run() {
                Initial(width, height);
            }
        });
        th.start();

    }

    if (resultCode == RESULT_OK
            && requestCode == Global.REQUEST_CODE_PICK_FILE_OR_DIRECTORY_FROM_PLATFORM_CONECTOR) {
        if (resultCode == android.app.Activity.RESULT_OK && data != null) {
            // obtain the filename
            Uri fileUri = data.getData();
            if (fileUri != null) {
                String filePath = fileUri.getPath();
                if (filePath != null) {
                    if (getFolderReturnListener != null)
                        getFolderReturnListener.getFolderReturn(filePath);
                }
            }
        }
        return;

    }

}

From source file:me.tb.player.SkeletonActivity.java

public void onShareClick() {
    Resources resources = getResources();
    String type = "image/*";
    String mediaPath = Environment.getExternalStorageDirectory() + "/game_icon1.png";

    // Create the URI from the media
    File media = new File(mediaPath);
    Uri uri = Uri.fromFile(media);//from w  w w.ja v  a  2  s.  c  o m

    Intent emailIntent = new Intent();
    emailIntent.setAction(Intent.ACTION_SEND);
    // Native email client doesn't currently support HTML, but it doesn't hurt to try in case they fix it
    emailIntent.putExtra(Intent.EXTRA_TEXT,
            "Download in Google Play Store\nhttps://play.google.com/store/apps/details?id=me.tb.player");
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Play Word Bandit - Multiplayer");
    emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
    emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    emailIntent.setType(type);

    PackageManager pm = getPackageManager();
    Intent sendIntent = new Intent(Intent.ACTION_SEND);
    sendIntent.setType(type);

    Intent openInChooser = Intent.createChooser(emailIntent, resources.getString(R.string.share_chooser_text));

    List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
    List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();
    for (int i = 0; i < resInfo.size(); i++) {
        // Extract the label, append it, and repackage it in a LabeledIntent
        ResolveInfo ri = resInfo.get(i);
        String packageName = ri.activityInfo.packageName;
        if (packageName.contains("com.google.android.gm")) {
            emailIntent.setPackage(packageName);
        } else if (packageName.contains("twitter") || packageName.contains("facebook.katana")
                || packageName.contains("com.instagram.android")) {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
            intent.setAction(Intent.ACTION_SEND);
            intent.setType(type);
            // Add the URI and the caption to the Intent.
            intent.putExtra(Intent.EXTRA_STREAM, uri);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

            if (packageName.contains("twitter") || packageName.contains("instagram")) {
                intent.putExtra(Intent.EXTRA_TEXT, shareMessageCombo
                        + "\nDownload now https://play.google.com/store/apps/details?id=me.tb.player");
            }
            intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
        }
    }

    // convert intentList to array
    LabeledIntent[] extraIntents = intentList.toArray(new LabeledIntent[intentList.size()]);

    openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
    startActivity(openInChooser);
}

From source file:com.android.dialer.voicemail.VoicemailPlaybackPresenter.java

private Intent getShareIntent(Uri voicemailFileUri) {
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, voicemailFileUri);
    shareIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    shareIntent.setType(mContext.getContentResolver().getType(voicemailFileUri));
    return shareIntent;
}