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.team.formal.eyeshopping.MainActivity.java

public void startCamera() {
    if (PermissionUtils.requestPermission(this, CAMERA_PERMISSIONS_REQUEST,
            Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA)) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        Uri photoUri = FileProvider.getUriForFile(getApplicationContext(),
                getApplicationContext().getPackageName() + ".provider", getCameraFile());
        intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        startActivityForResult(intent, CAMERA_IMAGE_REQUEST);
    }//from w w w  . j ava  2 s  . co  m
}

From source file:com.HumanDecisionSupportSystemsLaboratory.DD_P2P.SafeProfileFragment.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
    if (peer.getSK() == null) {
        return;//from w w w. ja va2 s . c  o  m
    }
    if (resultCode == Activity.RESULT_OK && resultData != null) {
        Uri uri = null;

        if (requestCode == SELECT_PROFILE_PHOTO) {
            uri = resultData.getData();
            Log.i("Uri", "Uri: " + uri.toString());
        } else if (requestCode == SELECT_PPROFILE_PHOTO_KITKAT) {
            uri = resultData.getData();
            Log.i("Uri_kitkat", "Uri: " + uri.toString());
            final int takeFlags = resultData.getFlags()
                    & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            // Check for the freshest data.
            getActivity().getContentResolver().takePersistableUriPermission(uri, takeFlags);
        }

        selectedImagePath = FileUtils.getPath(getActivity(), uri);
        Log.i("path", "path: " + selectedImagePath);

        selectImageFile = new File(selectedImagePath);

        /*         Bitmap bmp = BitmapFactory.decodeFile(selectedImagePath);*/
        // String strFromBmp = PhotoUtil.BitmapToString(bmp);

        Bitmap bmp = PhotoUtil.decodeSampledBitmapFromFile(selectedImagePath, 80, 80);
        //TODO fix the loading image
        byte[] icon;
        icon = PhotoUtil.BitmapToByteArray(bmp, 100);
        /*      int quality = 100;*/
        Log.i(TAG, "SafeProfile: Icon length=" + icon.length);
        /*DD.MAX_PEER_ICON_LENGTH = 30000;
        while (icon.length > DD.MAX_PEER_ICON_LENGTH && quality > 0) {
           quality -= 5;
           icon = PhotoUtil.BitmapToByteArray(bmp, quality);
           Log.i(TAG, "SafeProfile: Icon length=" + icon.length
          + " quality=" + quality);
        }
        Log.i(TAG, "SafeProfile: Icon length=" + icon.length + " quality="
              + quality);// Util.stringSignatureFromByte(icon));
        */ if (peer != null) {
            peer = D_Peer.getPeerByPeer_Keep(peer);
        }
        if (peer != null) {
            if (peer.getSK() != null) {
                if (peer.setIcon(icon)) {
                    peer.sign();
                    peer.storeRequest();
                }
                peer.releaseReference();
            }
        }

        imgbut.setImageBitmap(bmp);

    }
}

From source file:org.telegram.ui.Components.ImageUpdater.java

public void openCamera() {
    if (parentFragment == null || parentFragment.getParentActivity() == null) {
        return;/*from   w  w w  .  j a  v a  2  s.c  o m*/
    }
    try {
        if (Build.VERSION.SDK_INT >= 23 && parentFragment.getParentActivity()
                .checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            parentFragment.getParentActivity().requestPermissions(new String[] { Manifest.permission.CAMERA },
                    19);
            return;
        }
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File image = AndroidUtilities.generatePicturePath();
        if (image != null) {
            if (Build.VERSION.SDK_INT >= 24) {
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(
                        parentFragment.getParentActivity(), BuildConfig.APPLICATION_ID + ".provider", image));
                takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            } else {
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image));
            }
            currentPicturePath = image.getAbsolutePath();
        }
        parentFragment.startActivityForResult(takePictureIntent, 13);
    } catch (Exception e) {
        FileLog.e(e);
    }
}

From source file:com.android.messaging.ui.UIIntentsImpl.java

@Override
public void launchFullScreenVideoViewer(final Context context, final Uri videoUri) {
    final Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

    // So we don't see "surrounding" images in Gallery
    intent.putExtra("SingleItemOnly", true);
    intent.setDataAndType(videoUri, ContentType.VIDEO_UNSPECIFIED);
    startExternalActivity(context, intent);
}

From source file:net.opendasharchive.openarchive.ReviewMediaActivity.java

private void showMedia() {

    if (mMedia.mimeType.startsWith("image")) {
        ArrayList<Uri> list = new ArrayList<>();
        list.add(Uri.parse(mMedia.getOriginalFilePath()));
        new ImageViewer.Builder(this, list).setStartPosition(0).show();
    } else {/* w  w w .java  2 s  .c  om*/
        Intent viewMediaIntent = new Intent();
        viewMediaIntent.setAction(Intent.ACTION_VIEW);

        Uri uriFile = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider",
                FileUtils.getFile(this, Uri.parse(mMedia.getOriginalFilePath())));

        viewMediaIntent.setDataAndType(uriFile, mMedia.getMimeType());
        viewMediaIntent.putExtra(Intent.EXTRA_STREAM, uriFile);
        viewMediaIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

        startActivity(viewMediaIntent);
    }
}

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

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static void grandPermission5(Context ctx, Uri data) {
    DocumentFile docPath = DocumentFile.fromTreeUri(ctx, data);
    if (docPath != null) {
        final ContentResolver resolver = ctx.getContentResolver();
        resolver.takePersistableUriPermission(data,
                Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

    }/*from www.j  ava 2 s  .  c  o m*/
}

From source file:eu.alefzero.owncloud.ui.fragment.FileDetailFragment.java

/**
 * Updates the view with all relevant details about that file.
 *///w  w w  . j  ava2 s .c  om
public void updateFileDetails() {

    if (mFile != null && mAccount != null && mLayout == R.layout.file_details_fragment) {

        Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn);
        // set file details
        setFilename(mFile.getFileName());
        setFiletype(DisplayUtils.convertMIMEtoPrettyPrint(mFile.getMimetype()));
        setFilesize(mFile.getFileLength());
        if (ocVersionSupportsTimeCreated()) {
            setTimeCreated(mFile.getCreationTimestamp());
        }

        setTimeModified(mFile.getModificationTimestamp());

        CheckBox cb = (CheckBox) getView().findViewById(R.id.fdKeepInSync);
        cb.setChecked(mFile.keepInSync());

        if (mFile.getStoragePath() != null) {
            // Update preview
            if (mFile.getMimetype().startsWith("image/")) {
                BitmapLoader bl = new BitmapLoader();
                bl.execute(new String[] { mFile.getStoragePath() });
            }

            // Change download button to open button
            downloadButton.setText(R.string.filedetails_open);
            downloadButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    String storagePath = mFile.getStoragePath();
                    String encodedStoragePath = WebdavUtils.encodePath(storagePath);
                    try {
                        Intent i = new Intent(Intent.ACTION_VIEW);
                        i.setDataAndType(Uri.parse("file://" + encodedStoragePath), mFile.getMimetype());
                        i.setFlags(
                                Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                        startActivity(i);

                    } catch (Throwable t) {
                        Log.e(TAG,
                                "Fail when trying to open with the mimeType provided from the ownCloud server: "
                                        + mFile.getMimetype());
                        boolean toastIt = true;
                        String mimeType = "";
                        try {
                            Intent i = new Intent(Intent.ACTION_VIEW);
                            mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
                                    storagePath.substring(storagePath.lastIndexOf('.') + 1));
                            if (mimeType != null && !mimeType.equals(mFile.getMimetype())) {
                                i.setDataAndType(Uri.parse("file://" + encodedStoragePath), mimeType);
                                i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
                                        | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                                startActivity(i);
                                toastIt = false;
                            }

                        } catch (IndexOutOfBoundsException e) {
                            Log.e(TAG,
                                    "Trying to find out MIME type of a file without extension: " + storagePath);

                        } catch (ActivityNotFoundException e) {
                            Log.e(TAG, "No activity found to handle: " + storagePath + " with MIME type "
                                    + mimeType + " obtained from extension");

                        } catch (Throwable th) {
                            Log.e(TAG, "Unexpected problem when opening: " + storagePath, th);

                        } finally {
                            if (toastIt) {
                                Toast.makeText(getActivity(),
                                        "There is no application to handle file " + mFile.getFileName(),
                                        Toast.LENGTH_SHORT).show();
                            }
                        }

                    }
                }
            });
        } else {
            // Make download button effective
            downloadButton.setOnClickListener(this);
        }
    }
}

From source file:nuclei.ui.share.PackageTargetManager.java

protected void onSetFileProvider(Context context, String packageName, String authority, Intent intent) {
    if (mUri != null || mFile != null) {
        Uri uri = mUri;//  w  w  w. ja  v a  2 s  .c o  m
        String type = "*/*";
        if (mFile != null) {
            uri = FileProvider.getUriForFile(context, authority, mFile);
            final int lastDot = mFile.getName().lastIndexOf('.');
            if (lastDot >= 0) {
                String extension = mFile.getName().substring(lastDot + 1);
                String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
                if (mimeType != null)
                    type = mimeType;
            }
        }
        intent.setDataAndType(intent.getData(), type);
        intent.putExtra(Intent.EXTRA_STREAM, uri);
        if (packageName != null) {
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            context.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }
    } else {
        intent.setType("text/plain");
    }
}

From source file:org.mklab.mikity.android.MainActivity.java

/**
 * This is called after the file manager finished. {@inheritDoc}
 *//* ww w .j  a v a2  s.c om*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent resultData) {
    super.onActivityResult(requestCode, resultCode, resultData);

    switch (requestCode) {
    case REQUEST_CODE_LOAD_MODEL_DATA_FILE:
        if (resultCode == RESULT_OK && resultData != null) {
            final Uri uri = resultData.getData();

            final int takeFlags = resultData.getFlags()
                    & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            getContentResolver().takePersistableUriPermission(uri, takeFlags);

            loadModelData(uri);
        }
        break;
    case REQUEST_CODE_LOAD_SOURCE_DATA_FILE:
        if (resultCode == RESULT_OK && resultData != null) {
            final Uri uri = resultData.getData();
            loadSourceData(uri, this.sourceIdForIntent);
        }
        break;
    case REQUEST_CODE_SAVE_MODEL_DATA_FILE:
        if (resultCode == RESULT_OK && resultData != null) {
            final Uri uri = resultData.getData();
            saveAsModelData(uri);
        }
        break;
    default:
        break;
    }
}

From source file:com.brq.wallet.activity.export.BackupToPdfActivity.java

private void grantPermissions(Intent intent, Uri uri) {
    // grant permissions for all apps that can handle given intent
    // if we know of any malicious app that tries to intercept this,
    // we could block it here based on packageName
    PackageManager packageManager = Preconditions.checkNotNull(getPackageManager());
    List<ResolveInfo> resInfoList = packageManager.queryIntentActivities(intent,
            PackageManager.MATCH_DEFAULT_ONLY);
    for (ResolveInfo resolveInfo : resInfoList) {
        String packageName = resolveInfo.activityInfo.packageName;
        grantUriPermission(packageName, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
    }/* www  . j a  v  a  2 s  .c o  m*/
}