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.callrecorder.android.RecordingsAdapter.java

private void sendMail(String fileName) {
    DocumentFile file = FileHelper.getStorageFile(context).findFile(fileName);
    Uri uri = FileHelper.getContentUri(context, file.getUri());

    Intent sendIntent = new Intent(Intent.ACTION_SEND)
            .putExtra(Intent.EXTRA_SUBJECT, context.getString(R.string.mail_subject))
            .putExtra(Intent.EXTRA_TEXT, context.getString(R.string.mail_body))
            .putExtra(Intent.EXTRA_STREAM, uri).setData(uri).addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            .setType("audio/3gpp");

    context.startActivity(Intent.createChooser(sendIntent, context.getString(R.string.send_mail)));
}

From source file:es.danirod.rectball.android.AndroidSharing.java

/**
 * This method builds an Intent for sharing the screenshot with other apps
 * present in the mobile phone. The URI should point to the screenshot.
 *
 * @param uri  URI that points to the screenshot to be saved
 * @param message  message to be present in the screenshot
 * @return  the Intent ready to be passed to the activity.
 *///w ww. ja  v a 2 s .c o  m
private Intent shareScreenshotURI(Uri uri, CharSequence message) {
    Intent sharingIntent = new Intent();
    sharingIntent.setAction(Intent.ACTION_SEND);
    sharingIntent.putExtra(Intent.EXTRA_SUBJECT, message);
    sharingIntent.putExtra(Intent.EXTRA_TEXT, message);
    sharingIntent.setType("image/png");
    sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
    sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    return sharingIntent;
}

From source file:org.eyeseetea.malariacare.fragments.AVFragment.java

@Override
public void OpenMedia(String resourcePath) {
    Intent implicitIntent = new Intent();
    implicitIntent.setAction(Intent.ACTION_VIEW);
    File file = new File(resourcePath);
    Uri contentUri = FileProvider.getUriForFile(getActivity(),
            BuildConfig.APPLICATION_ID + ".layout.adapters.dashboard.AVAdapter", file);

    implicitIntent.setDataAndType(contentUri,
            PreferencesState.getInstance().getContext().getContentResolver().getType(contentUri));
    implicitIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    implicitIntent.putExtra(Intent.EXTRA_STREAM, contentUri);

    if (isThereAnAppThatCanHandleThis(implicitIntent, getActivity())) {
        getActivity().startActivity(Intent.createChooser(implicitIntent,
                PreferencesState.getInstance().getContext().getString(R.string.feedback_view_image)));
    } else {//from w w  w . j  av  a2 s.  c  om
        showToast(R.string.error_unable_to_find_app_than_can_open_file, getActivity());
    }
}

From source file:com.gnuroot.rsinstaller.RSLauncherMain.java

/**
 * GNURoot Debian expects the following extras from install intents:
 *    1. launchType: This can be either launchTerm or launchXTerm. The command that is to be run after installation
 *       dictates this selection./*from  ww w  . j  a  v  a 2 s.c o m*/
 *   2. command: This is the command that will be executed in proot after installation. Often, this will be a
 *      script stored in your custom tar file to install additional packages if needed or to execute the extension.
 *   3. customTar: This is the custom tar file you've created for your extension.
 * @return
  */
private Intent getLaunchIntent() {
    String command;
    Intent installIntent = new Intent("com.gnuroot.debian.LAUNCH");
    installIntent.setComponent(new ComponentName("com.gnuroot.debian", "com.gnuroot.debian.GNURootMain"));
    installIntent.addCategory(Intent.CATEGORY_DEFAULT);
    installIntent.putExtra("launchType", "launchXTerm");
    command = "#!/bin/bash\n"
            + "if [ ! -f /support/.rs_custom_passed ] || [ ! -f /support/.rs_updated ]; then\n"
            + "  /support/untargz rs_custom /support/rs_custom.tar.gz\n" + "fi\n"
            + "if [ -f /support/.rs_custom_passed ]; then\n"
            + "  if [ ! -f /support/.rs_script_passed ]; then\n"
            + "    sudo /support/blockingScript rs_script /support/oldschoolrs_install.sh\n" + "  fi\n"
            + "  if [ ! -f /support/.rs_script_updated ]; then\n"
            + "    sudo mv /support/rs_update /usr/bin/oldschoolrs\n" + "    if [ $? == 0 ]; then\n"
            + "      touch /support/.rs_script_updated\n" + "      sudo chmod 755 /usr/bin/oldschoolrs\n"
            + "    fi\n" + "  fi\n" + "  if [ -f /support/.rs_script_passed ]; then\n"
            + "    /usr/bin/oldschoolrs\n" + "  fi\n" + "fi\n";
    installIntent.putExtra("command", command);
    installIntent.putExtra("packageName", "com.gnuroot.rsinstaller");
    installIntent.putExtra("GNURootVersion", GNURootVersion);
    installIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    installIntent.setData(getTarUri());
    return installIntent;
}

From source file:com.callrecorder.android.RecordingsAdapter.java

private void startPlayExternal(String fileName) {
    DocumentFile file = FileHelper.getStorageFile(context).findFile(fileName);
    Uri uri = FileHelper.getContentUri(context, file.getUri());

    context.startActivity(new Intent().setAction(Intent.ACTION_VIEW).setData(uri)
            .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION).setType("audio/3gpp"));
}

From source file:com.silentcircle.contacts.utils.ContactPhotoUtils19.java

/**
 * Adds common extras to gallery intents.
 *
 * @param intent The intent to add extras to.
 * @param photoUri The uri of the file to save the image to.
 *///  w  w  w .  j a va  2  s.c om
@TargetApi(Build.VERSION_CODES.KITKAT)
public static void addPhotoPickerExtras(Intent intent, Uri photoUri) {
    intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
    intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.setClipData(ClipData.newRawUri(MediaStore.EXTRA_OUTPUT, photoUri));
}

From source file:us.rader.wyfy.QrCodeFragment.java

/**
 * Save QR code to a file and then send a sharing {@link Intent} wrapped in
 * a chooser//  w w  w  .  j  av a2 s.com
 */
public void shareQrCode() {

    try {

        FragmentActivity activity = getActivity();
        Bitmap bitmap = wifiSettings.getQrCode(Color.BLACK, Color.WHITE, getQrCodeSize());
        File file = activity.getFileStreamPath("wyfy_qr.png"); //$NON-NLS-1$
        FileOutputStream stream = activity.openFileOutput(file.getName(), 0);

        try {

            bitmap.compress(CompressFormat.PNG, 100, stream);

        } finally {

            stream.close();

        }

        String label = getString(R.string.share_label);
        Uri uri = FileProvider.getContentUri(getString(R.string.provider_authority_file), file.getName());
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setData(uri);
        intent.setType(FileProvider.getMimeType(uri));
        intent.putExtra(Intent.EXTRA_STREAM, uri);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        Intent chooser = Intent.createChooser(intent, label);
        startActivity(chooser);

    } catch (Exception e) {

        // ignore errors here

    }
}

From source file:com.giovanniterlingen.windesheim.view.DownloadsActivity.java

private void updateFilesList() {
    final File path = Environment.getExternalStoragePublicDirectory(
            ApplicationLoader.applicationContext.getResources().getString(R.string.app_name));
    File files[] = path.listFiles();
    if (files != null && files.length > 0) {
        List<NatschoolContent> contents = new ArrayList<>();
        for (File f : files) {
            if (!f.isDirectory()) {
                contents.add(new NatschoolContent(f.getName()));
            }/*from w  w w . java 2 s. c o m*/
        }
        if (contents.size() == 0) {
            showEmptyTextview();
        } else {
            hideEmptyTextview();
        }
        if (recyclerView == null) {
            recyclerView = findViewById(R.id.downloads_recyclerview);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
        }
        recyclerView.setAdapter(new NatschoolContentAdapter(this, contents) {
            @Override
            protected void onContentClick(NatschoolContent content, int position) {
                Uri uri;
                File file = new File(path.getAbsolutePath() + File.separator + content.name);
                Intent target = new Intent(Intent.ACTION_VIEW);
                if (android.os.Build.VERSION.SDK_INT >= 24) {
                    uri = FileProvider.getUriForFile(DownloadsActivity.this,
                            "com.giovanniterlingen.windesheim.provider", file);
                    target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_GRANT_READ_URI_PERMISSION);
                } else {
                    uri = Uri.fromFile(file);
                    target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                }
                String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(uri.toString());
                String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
                target.setDataAndType(uri, mimetype);

                try {
                    startActivity(target);
                } catch (ActivityNotFoundException e) {
                    if (view != null) {
                        Snackbar snackbar = Snackbar.make(view, getResources().getString(R.string.no_app_found),
                                Snackbar.LENGTH_SHORT);
                        snackbar.show();
                    }
                }
            }
        });
    } else {
        showEmptyTextview();
    }
}

From source file:freed.ActivityAbstract.java

@TargetApi(VERSION_CODES.KITKAT)
@Override/*from   w w w .j  a  v  a2s.com*/
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
        // The document selected by the user won't be returned in the intent.
        // Instead, a URI to that document will be contained in the return intent
        // provided to this method as a parameter.
        // Pull that URI using resultData.getData().
        Uri uri = null;
        if (data != null) {
            uri = data.getData();
            int takeFlags = data.getFlags()
                    & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            // Check for the freshest data.

            getContentResolver().takePersistableUriPermission(uri, takeFlags);
            appSettingsManager.SetBaseFolder(uri.toString());
            if (resultCallback != null) {
                resultCallback.onActivityResultCallback(uri);
                resultCallback = null;
            }
        }
    }
}

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

@SuppressLint("NewApi")
@Override//w ww .j  a  v  a 2  s  .  c o m
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
    if (resultCode == Activity.RESULT_OK && resultData != null) {
        Uri uri = null;

        if (requestCode == SELECT_PHOTO) {
            uri = resultData.getData();
            Log.i("Uri", "Uri: " + uri.toString());
        } else if (requestCode == SELECT_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);
        //File testFile = new File("file://storage/emulated/0/DCIM/hobbit.bmp");

        boolean success;

        String[] selected = new String[1];
        DD_Address adr = new DD_Address(peer);
        try {
            //util.EmbedInMedia.DEBUG = true;
            Log.i("success_embed", "success_embed 1: " + selectImageFile);
            success = DD.embedPeerInBMP(selectImageFile, selected, adr);
            Log.i("success_embed", "success_embed 2: " + success);
            if (success == true) {
                Toast.makeText(getActivity(), "Export success!", Toast.LENGTH_SHORT).show();
            } else
                Toast.makeText(getActivity(), "Unable to export:" + selected[0], Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Toast.makeText(getActivity(), "Unable to export!", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }

    }

    /*      if (resultCode == Activity.RESULT_OK && resultData != null) {
    Uri uri = null;
            
    if (requestCode == PK_SELECT_PHOTO) {
        uri = resultData.getData();
        Log.i("Uri", "Uri: " + uri.toString());
    } else if (requestCode == PK_SELECT_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);
    //File testFile = new File("file://storage/emulated/0/DCIM/hobbit.bmp");
            
    boolean success;    
            
    try {
       //util.EmbedInMedia.DEBUG = true;
       success = saveSK(peer, selectImageFile);
       Log.i("success_embed", "success_embed: " + success); 
     if (success == true) {
         Toast.makeText(getActivity(), "Export success!", Toast.LENGTH_SHORT).show();
     } else 
         Toast.makeText(getActivity(), "Unable to export!", Toast.LENGTH_SHORT).show();  
    }
              catch (Exception e) {
     Toast.makeText(getActivity(), "Unable to export!", Toast.LENGTH_SHORT).show();
     e.printStackTrace();
              } 
                   
          }*/
    super.onActivityResult(requestCode, resultCode, resultData);
}