List of usage examples for android.content Intent FLAG_GRANT_READ_URI_PERMISSION
int FLAG_GRANT_READ_URI_PERMISSION
To view the source code for android.content Intent FLAG_GRANT_READ_URI_PERMISSION.
Click Source Link
From source file:ren.qinc.markdowneditors.view.EditorActivity.java
private void getIntentData() { Intent intent = this.getIntent(); int flags = intent.getFlags(); if ((flags & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0) { if (intent.getAction() != null && Intent.ACTION_VIEW.equals(intent.getAction())) { if (SCHEME_FILE.equals(intent.getScheme())) { // String type = getIntent().getType(); // mImportingUri=file:///storage/emulated/0/Vlog.xml intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); Uri uri = intent.getData(); if (uri != null && SCHEME_FILE.equalsIgnoreCase(uri.getScheme())) { // currentFilePath = FileUtils.uri2FilePath(getBaseContext(), uri); }/*from w ww. j av a2 s. com*/ } } } }
From source file:com.commonsware.android.diceware.PassphraseFragment.java
private static boolean obtainDurablePermission(ContentResolver resolver, Uri document) { boolean weHaveDurablePermission = false; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { int perms = Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION; try {/* w ww .j ava 2 s.co m*/ resolver.takePersistableUriPermission(document, perms); for (UriPermission perm : resolver.getPersistedUriPermissions()) { if (perm.getUri().equals(document)) { weHaveDurablePermission = true; } } } catch (SecurityException e) { // OK, we were not offered any persistable permissions } } return (weHaveDurablePermission); }
From source file:gov.wa.wsdot.android.wsdot.ui.camera.CameraImageFragment.java
private Intent createShareIntent() { File f = new File(getActivity().getFilesDir(), mCameraName); ContentValues values = new ContentValues(2); values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); values.put(MediaStore.Images.Media.DATA, f.getAbsolutePath()); Uri uri = getActivity().getContentResolver().insert(MediaStore.Images.Media.INTERNAL_CONTENT_URI, values); Intent shareIntent = new Intent(Intent.ACTION_SEND); shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); shareIntent.setType("image/jpeg"); shareIntent.putExtra(Intent.EXTRA_TEXT, mTitle); shareIntent.putExtra(Intent.EXTRA_SUBJECT, mTitle); shareIntent.putExtra(Intent.EXTRA_STREAM, uri); return shareIntent; }
From source file:com.android.mail.browse.AttachmentActionHandler.java
public void shareAttachment() { if (mAttachment.contentUri == null) { return;//www.j a va 2 s . c om } Intent intent = new Intent(Intent.ACTION_SEND); intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); final Uri uri = Utils.normalizeUri(mAttachment.contentUri); intent.putExtra(Intent.EXTRA_STREAM, uri); intent.setType(Utils.normalizeMimeType(mAttachment.getContentType())); try { mContext.startActivity(intent); } catch (ActivityNotFoundException e) { // couldn't find activity for SEND intent LogUtils.e(LOG_TAG, "Couldn't find Activity for intent", e); } }
From source file:com.jefftharris.passwdsafe.StorageFileListFragment.java
@Override public Loader<Cursor> onCreateLoader(int i, Bundle bundle) { return new AsyncTaskLoader<Cursor>(getActivity()) { /** Handle when the loader is reset */ @Override/*w w w . j a v a 2 s . c o m*/ protected void onReset() { super.onReset(); onStopLoading(); } /** Handle when the loader is started */ @Override protected void onStartLoading() { forceLoad(); } /** Handle when the loader is stopped */ @Override protected void onStopLoading() { cancelLoad(); } /** Load the files in the background */ @Override public Cursor loadInBackground() { PasswdSafeUtil.dbginfo(TAG, "loadInBackground"); int flags = Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION; ContentResolver cr = getContext().getContentResolver(); List<Uri> permUris = ApiCompat.getPersistedUriPermissions(cr); for (Uri permUri : permUris) { PasswdSafeUtil.dbginfo(TAG, "Checking persist perm %s", permUri); Cursor cursor = null; try { cursor = cr.query(permUri, null, null, null, null); if ((cursor != null) && (cursor.moveToFirst())) { ApiCompat.takePersistableUriPermission(cr, permUri, flags); } else { ApiCompat.releasePersistableUriPermission(cr, permUri, flags); itsRecentFilesDb.removeUri(permUri); } } catch (Exception e) { Log.e(TAG, "File remove error: " + permUri, e); } finally { if (cursor != null) { cursor.close(); } } } try { return itsRecentFilesDb.queryFiles(); } catch (Exception e) { Log.e(TAG, "Files load error", e); } return null; } }; }
From source file:com.zhongzilu.bit100.view.activity.EditorActivity.java
private void getIntentData() { Intent intent = this.getIntent(); int flags = intent.getFlags(); if ((flags & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0) { if (intent.getAction() != null && Intent.ACTION_VIEW.equals(intent.getAction())) { if (SCHEME_FILE.equals(intent.getScheme())) { // // String type = getIntent().getType(); // mImportingUri=file:///storage/emulated/0/Vlog.xml intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); Uri uri = intent.getData(); if (uri != null && SCHEME_FILE.equalsIgnoreCase(uri.getScheme())) { // currentFilePath = FileUtils.uri2FilePath(getBaseContext(), uri); }/* ww w . j ava 2 s . c om*/ } } } }
From source file:com.github.dfa.diaspora_android.ui.ContextMenuWebView.java
@Override protected void onCreateContextMenu(ContextMenu menu) { super.onCreateContextMenu(menu); HitTestResult result = getHitTestResult(); MenuItem.OnMenuItemClickListener handler = new MenuItem.OnMenuItemClickListener() { public boolean onMenuItemClick(MenuItem item) { HitTestResult result = getHitTestResult(); String url = result.getExtra(); switch (item.getItemId()) { //Save image to external memory case ID_SAVE_IMAGE: { boolean writeToStoragePermitted = true; if (android.os.Build.VERSION.SDK_INT >= 23) { int hasWRITE_EXTERNAL_STORAGE = parentActivity .checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE); if (hasWRITE_EXTERNAL_STORAGE != PackageManager.PERMISSION_GRANTED) { writeToStoragePermitted = false; if (!parentActivity.shouldShowRequestPermissionRationale( Manifest.permission.WRITE_EXTERNAL_STORAGE)) { new AlertDialog.Builder(parentActivity).setMessage(R.string.permissions_image) .setPositiveButton(context.getText(android.R.string.yes), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (android.os.Build.VERSION.SDK_INT >= 23) parentActivity.requestPermissions(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, MainActivity.REQUEST_CODE__ACCESS_EXTERNAL_STORAGE); } }) .setNegativeButton(context.getText(android.R.string.no), null).show(); }/*from ww w .ja v a2 s . c o m*/ parentActivity.requestPermissions( new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, MainActivity.REQUEST_CODE__ACCESS_EXTERNAL_STORAGE); } } if (writeToStoragePermitted) { if (url != null) { Uri source = Uri.parse(url); DownloadManager.Request request = new DownloadManager.Request(source); File destinationFile = new File(Environment.getExternalStorageDirectory() + "/Pictures/Diaspora/" + System.currentTimeMillis() + ".png"); request.setDestinationUri(Uri.fromFile(destinationFile)); ((DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE)).enqueue(request); Toast.makeText(context, context.getText(R.string.share__toast_saved_image_to_location) + " " + destinationFile.getAbsolutePath(), Toast.LENGTH_LONG).show(); } } } break; case ID_SHARE_IMAGE: if (url != null) { boolean writeToStoragePermitted = true; if (android.os.Build.VERSION.SDK_INT >= 23) { int hasWRITE_EXTERNAL_STORAGE = parentActivity .checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE); if (hasWRITE_EXTERNAL_STORAGE != PackageManager.PERMISSION_GRANTED) { writeToStoragePermitted = false; if (!parentActivity.shouldShowRequestPermissionRationale( Manifest.permission.WRITE_EXTERNAL_STORAGE)) { new AlertDialog.Builder(parentActivity).setMessage(R.string.permissions_image) .setPositiveButton(context.getText(android.R.string.yes), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (android.os.Build.VERSION.SDK_INT >= 23) parentActivity.requestPermissions(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, MainActivity.REQUEST_CODE__ACCESS_EXTERNAL_STORAGE); } }) .setNegativeButton(context.getText(android.R.string.no), null).show(); } else { parentActivity.requestPermissions( new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, MainActivity.REQUEST_CODE__ACCESS_EXTERNAL_STORAGE); } } } if (writeToStoragePermitted) { final Uri local = Uri.parse(Environment.getExternalStorageDirectory() + "/Pictures/Diaspora/" + System.currentTimeMillis() + ".png"); new ImageDownloadTask(null, local.getPath()) { @Override protected void onPostExecute(Bitmap result) { Uri myUri = Uri.fromFile(new File(local.getPath())); Intent sharingIntent = new Intent(); sharingIntent.setAction(Intent.ACTION_SEND); sharingIntent.putExtra(Intent.EXTRA_STREAM, myUri); sharingIntent.setType("image/png"); sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); context.startActivity(Intent.createChooser(sharingIntent, getResources().getString(R.string.action_share_dotdotdot))); } }.execute(url); } } else { Toast.makeText(context, "Cannot share image: url is null", Toast.LENGTH_SHORT).show(); } break; case ID_IMAGE_EXTERNAL_BROWSER: if (url != null) { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); context.startActivity(intent); } break; //Copy url to clipboard case ID_COPY_LINK: if (url != null) { ClipboardManager clipboard = (ClipboardManager) context .getSystemService(Context.CLIPBOARD_SERVICE); clipboard.setPrimaryClip(ClipData.newPlainText("text", url)); Toast.makeText(context, R.string.share__toast_link_address_copied, Toast.LENGTH_SHORT) .show(); } break; //Try to share link to other apps case ID_SHARE_LINK: if (url != null) { Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_TEXT, url); sendIntent.setType("text/plain"); context.startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.context_menu_share_link))); } break; } return true; } }; //Build context menu if (result.getType() == HitTestResult.IMAGE_TYPE || result.getType() == HitTestResult.SRC_IMAGE_ANCHOR_TYPE) { // Menu options for an image. menu.setHeaderTitle(result.getExtra()); menu.add(0, ID_SAVE_IMAGE, 0, context.getString(R.string.context_menu_save_image)) .setOnMenuItemClickListener(handler); menu.add(0, ID_IMAGE_EXTERNAL_BROWSER, 0, context.getString(R.string.context_menu_open_external_browser)) .setOnMenuItemClickListener(handler); menu.add(0, ID_SHARE_IMAGE, 0, context.getString(R.string.context_menu_share_image)) .setOnMenuItemClickListener(handler); } else if (result.getType() == HitTestResult.ANCHOR_TYPE || result.getType() == HitTestResult.SRC_ANCHOR_TYPE) { // Menu options for a hyperlink. menu.setHeaderTitle(result.getExtra()); menu.add(0, ID_COPY_LINK, 0, context.getString(R.string.context_menu_copy_link)) .setOnMenuItemClickListener(handler); menu.add(0, ID_SHARE_LINK, 0, context.getString(R.string.context_menu_share_link)) .setOnMenuItemClickListener(handler); } }
From source file:com.dm.material.dashboard.candybar.helpers.IconsHelper.java
public static void selectIcon(@NonNull Context context, int action, Icon icon) { if (action == IntentHelper.ICON_PICKER) { Intent intent = new Intent(); Bitmap bitmap = ImageLoader.getInstance().loadImageSync("drawable://" + icon.getRes(), ImageConfig.getRawImageOptions().build()); intent.putExtra("icon", bitmap); ((AppCompatActivity) context).setResult(bitmap != null ? Activity.RESULT_OK : Activity.RESULT_CANCELED, intent);/*from w w w . ja va 2 s . com*/ ((AppCompatActivity) context).finish(); } else if (action == IntentHelper.IMAGE_PICKER) { Intent intent = new Intent(); Bitmap bitmap = ImageLoader.getInstance().loadImageSync("drawable://" + icon.getRes(), ImageConfig.getRawImageOptions().build()); if (bitmap != null) { File file = new File(context.getCacheDir(), icon.getTitle() + ".png"); FileOutputStream outStream; try { outStream = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream); outStream.flush(); outStream.close(); Uri uri = FileHelper.getUriFromFile(context, context.getPackageName(), file); if (uri == null) uri = Uri.fromFile(file); intent.putExtra(Intent.EXTRA_STREAM, uri); intent.setData(uri); intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); } catch (Exception | OutOfMemoryError e) { LogUtil.e(Log.getStackTraceString(e)); } intent.putExtra("return-data", false); } ((AppCompatActivity) context).setResult(bitmap != null ? Activity.RESULT_OK : Activity.RESULT_CANCELED, intent); ((AppCompatActivity) context).finish(); } else { IconPreviewFragment.showIconPreview(((AppCompatActivity) context).getSupportFragmentManager(), icon.getTitle(), icon.getRes()); } }
From source file:com.github.dfa.diaspora_android.web.ContextMenuWebView.java
@Override protected void onCreateContextMenu(ContextMenu menu) { super.onCreateContextMenu(menu); HitTestResult result = getHitTestResult(); MenuItem.OnMenuItemClickListener handler = new MenuItem.OnMenuItemClickListener() { public boolean onMenuItemClick(MenuItem item) { HitTestResult result = getHitTestResult(); String url = result.getExtra(); switch (item.getItemId()) { //Save image to external memory case ID_SAVE_IMAGE: { boolean writeToStoragePermitted = true; if (android.os.Build.VERSION.SDK_INT >= 23) { int hasWRITE_EXTERNAL_STORAGE = parentActivity .checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE); if (hasWRITE_EXTERNAL_STORAGE != PackageManager.PERMISSION_GRANTED) { writeToStoragePermitted = false; if (!parentActivity.shouldShowRequestPermissionRationale( Manifest.permission.WRITE_EXTERNAL_STORAGE)) { new AlertDialog.Builder(parentActivity).setMessage(R.string.permissions_image) .setPositiveButton(context.getText(android.R.string.yes), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (android.os.Build.VERSION.SDK_INT >= 23) parentActivity.requestPermissions(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, MainActivity.REQUEST_CODE__ACCESS_EXTERNAL_STORAGE); } }) .setNegativeButton(context.getText(android.R.string.no), null).show(); }/* w ww . j ava2s. com*/ parentActivity.requestPermissions( new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, MainActivity.REQUEST_CODE__ACCESS_EXTERNAL_STORAGE); } } if (writeToStoragePermitted) { if (url != null) { Uri source = Uri.parse(url); DownloadManager.Request request = new DownloadManager.Request(source); File destinationFile = new File(Environment.getExternalStorageDirectory() + "/Pictures/Diaspora/" + System.currentTimeMillis() + ".png"); request.setDestinationUri(Uri.fromFile(destinationFile)); ((DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE)).enqueue(request); Toast.makeText(context, context.getText(R.string.share__toast_saved_image_to_location) + " " + destinationFile.getAbsolutePath(), Toast.LENGTH_LONG).show(); } } } break; case ID_SHARE_IMAGE: if (url != null) { boolean writeToStoragePermitted = true; if (android.os.Build.VERSION.SDK_INT >= 23) { int hasWRITE_EXTERNAL_STORAGE = parentActivity .checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE); if (hasWRITE_EXTERNAL_STORAGE != PackageManager.PERMISSION_GRANTED) { writeToStoragePermitted = false; if (!parentActivity.shouldShowRequestPermissionRationale( Manifest.permission.WRITE_EXTERNAL_STORAGE)) { new AlertDialog.Builder(parentActivity).setMessage(R.string.permissions_image) .setPositiveButton(context.getText(android.R.string.yes), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (android.os.Build.VERSION.SDK_INT >= 23) parentActivity.requestPermissions(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, MainActivity.REQUEST_CODE__ACCESS_EXTERNAL_STORAGE); } }) .setNegativeButton(context.getText(android.R.string.no), null).show(); } else { parentActivity.requestPermissions( new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, MainActivity.REQUEST_CODE__ACCESS_EXTERNAL_STORAGE); } } } if (writeToStoragePermitted) { final Uri local = Uri.parse(Environment.getExternalStorageDirectory() + "/Pictures/Diaspora/" + System.currentTimeMillis() + ".png"); new ImageDownloadTask(null, local.getPath()) { @Override protected void onPostExecute(Bitmap result) { Uri myUri = Uri.fromFile(new File(local.getPath())); Intent sharingIntent = new Intent(); sharingIntent.setAction(Intent.ACTION_SEND); sharingIntent.putExtra(Intent.EXTRA_STREAM, myUri); sharingIntent.setType("image/png"); sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); context.startActivity(Intent.createChooser(sharingIntent, getResources().getString(R.string.action_share_dotdotdot))); } }.execute(url); } } else { Toast.makeText(context, "Cannot share image: url is null", Toast.LENGTH_SHORT).show(); } break; case ID_IMAGE_EXTERNAL_BROWSER: if (url != null) { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); context.startActivity(intent); } break; //Copy url to clipboard case ID_COPY_LINK: if (url != null) { ClipboardManager clipboard = (ClipboardManager) context .getSystemService(Context.CLIPBOARD_SERVICE); clipboard.setPrimaryClip(ClipData.newPlainText("text", url)); Toast.makeText(context, R.string.share__toast_link_address_copied, Toast.LENGTH_SHORT) .show(); } break; //Try to share link to other apps case ID_SHARE_LINK: if (url != null) { Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_TEXT, url); sendIntent.setType("text/plain"); context.startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.context_menu_share_link))); } break; } return true; } }; //Build context menu if (result.getType() == HitTestResult.IMAGE_TYPE || result.getType() == HitTestResult.SRC_IMAGE_ANCHOR_TYPE) { // Menu options for an image. menu.setHeaderTitle(result.getExtra()); menu.add(0, ID_SAVE_IMAGE, 0, context.getString(R.string.context_menu_save_image)) .setOnMenuItemClickListener(handler); menu.add(0, ID_IMAGE_EXTERNAL_BROWSER, 0, context.getString(R.string.context_menu_open_external_browser)) .setOnMenuItemClickListener(handler); menu.add(0, ID_SHARE_IMAGE, 0, context.getString(R.string.context_menu_share_image)) .setOnMenuItemClickListener(handler); } else if (result.getType() == HitTestResult.ANCHOR_TYPE || result.getType() == HitTestResult.SRC_ANCHOR_TYPE) { // Menu options for a hyperlink. menu.setHeaderTitle(result.getExtra()); menu.add(0, ID_COPY_LINK, 0, context.getString(R.string.context_menu_copy_link)) .setOnMenuItemClickListener(handler); menu.add(0, ID_SHARE_LINK, 0, context.getString(R.string.context_menu_share_link)) .setOnMenuItemClickListener(handler); } }
From source file:com.xbm.android.matisse.ui.MatisseActivity.java
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode != RESULT_OK) return;/* w w w. ja v a 2 s. c o m*/ if (requestCode == REQUEST_CODE_PREVIEW) { Bundle resultBundle = data.getBundleExtra(BasePreviewActivity.EXTRA_RESULT_BUNDLE); ArrayList<Item> selected = resultBundle.getParcelableArrayList(SelectedItemCollection.STATE_SELECTION); int collectionType = resultBundle.getInt(SelectedItemCollection.STATE_COLLECTION_TYPE, SelectedItemCollection.COLLECTION_UNDEFINED); if (data.getBooleanExtra(BasePreviewActivity.EXTRA_RESULT_APPLY, false)) { Intent result = new Intent(); ArrayList<Uri> selectedUris = new ArrayList<>(); ArrayList<String> selectedPaths = new ArrayList<>(); if (selected != null) { for (Item item : selected) { selectedUris.add(item.getContentUri()); selectedPaths.add(PathUtils.getPath(this, item.getContentUri())); } } result.putParcelableArrayListExtra(EXTRA_RESULT_SELECTION, selectedUris); result.putStringArrayListExtra(EXTRA_RESULT_SELECTION_PATH, selectedPaths); setResult(RESULT_OK, result); finish(); } else { mSelectedCollection.overwrite(selected, collectionType); Fragment mediaSelectionFragment = getSupportFragmentManager() .findFragmentByTag(MediaSelectionFragment.class.getSimpleName()); if (mediaSelectionFragment instanceof MediaSelectionFragment) { ((MediaSelectionFragment) mediaSelectionFragment).refreshMediaGrid(); } updateBottomToolbar(); } } else if (requestCode == REQUEST_CODE_CAPTURE) { // Just pass the data back to previous calling Activity. Uri contentUri = mMediaStoreCompat.getCurrentPhotoUri(); String path = mMediaStoreCompat.getCurrentPhotoPath(); ArrayList<Uri> selected = new ArrayList<>(); selected.add(contentUri); ArrayList<String> selectedPath = new ArrayList<>(); selectedPath.add(path); Intent result = new Intent(); result.putParcelableArrayListExtra(EXTRA_RESULT_SELECTION, selected); result.putStringArrayListExtra(EXTRA_RESULT_SELECTION_PATH, selectedPath); setResult(RESULT_OK, result); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) MatisseActivity.this.revokeUriPermission(contentUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION); finish(); } }