Example usage for android.content Intent FLAG_GRANT_WRITE_URI_PERMISSION

List of usage examples for android.content Intent FLAG_GRANT_WRITE_URI_PERMISSION

Introduction

In this page you can find the example usage for android.content Intent FLAG_GRANT_WRITE_URI_PERMISSION.

Prototype

int FLAG_GRANT_WRITE_URI_PERMISSION

To view the source code for android.content Intent FLAG_GRANT_WRITE_URI_PERMISSION.

Click Source Link

Document

If set, the recipient of this Intent will be granted permission to perform write operations on the URI in the Intent's data and any URIs specified in its ClipData.

Usage

From source file:com.commonsware.android.camcon.CameraContentDemoActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    if (savedInstanceState == null) {
        output = new File(new File(getFilesDir(), PHOTOS), FILENAME);

        if (output.exists()) {
            output.delete();//from ww w  . j a  va2 s .c o  m
        } else {
            output.getParentFile().mkdirs();
        }
    } else {
        output = (File) savedInstanceState.getSerializable(EXTRA_FILENAME);
    }

    outputUri = FileProvider.getUriForFile(this, AUTHORITY, output);

    if (savedInstanceState == null) {
        i.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            ClipData clip = ClipData.newUri(getContentResolver(), "A photo", outputUri);

            i.setClipData(clip);
            i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        } else {
            List<ResolveInfo> resInfoList = getPackageManager().queryIntentActivities(i,
                    PackageManager.MATCH_DEFAULT_ONLY);

            for (ResolveInfo resolveInfo : resInfoList) {
                String packageName = resolveInfo.activityInfo.packageName;
                grantUriPermission(packageName, outputUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            }
        }

        startActivityForResult(i, CONTENT_REQUEST);
    }
}

From source file:com.anthonymandra.support.v4.provider.DocumentsContractApi19.java

public static boolean canWrite(Context context, Uri self) {
    // Ignore if grant doesn't allow write
    if (context.checkCallingOrSelfUriPermission(self,
            Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != PackageManager.PERMISSION_GRANTED) {
        return false;
    }//from   ww w . j  a v  a 2s .c  o  m

    final String type = getRawType(context, self);
    final int flags = queryForInt(context, self, DocumentsContract.Document.COLUMN_FLAGS, 0);

    // Ignore documents without MIME
    if (TextUtils.isEmpty(type)) {
        return false;
    }

    // Deletable documents considered writable
    if ((flags & DocumentsContract.Document.FLAG_SUPPORTS_DELETE) != 0) {
        return true;
    }

    if (DocumentsContract.Document.MIME_TYPE_DIR.equals(type)
            && (flags & DocumentsContract.Document.FLAG_DIR_SUPPORTS_CREATE) != 0) {
        // Directories that allow create considered writable
        return true;
    } else if (!TextUtils.isEmpty(type) && (flags & DocumentsContract.Document.FLAG_SUPPORTS_WRITE) != 0) {
        // Writable normal files considered writable
        return true;
    }

    return false;
}

From source file:com.commonsware.android.videorecord.MainActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (savedInstanceState == null) {
        output = new File(new File(getFilesDir(), VIDEOS), FILENAME);

        if (output.exists()) {
            output.delete();//w  ww  .  j a va2  s  .c  o  m
        } else {
            output.getParentFile().mkdirs();
        }
    } else {
        output = (File) savedInstanceState.getSerializable(EXTRA_FILENAME);
    }

    outputUri = FileProvider.getUriForFile(this, AUTHORITY, output);

    if (savedInstanceState == null) {
        Intent i = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

        i.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
        i.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } else {
            List<ResolveInfo> resInfoList = getPackageManager().queryIntentActivities(i,
                    PackageManager.MATCH_DEFAULT_ONLY);

            for (ResolveInfo resolveInfo : resInfoList) {
                String packageName = resolveInfo.activityInfo.packageName;

                grantUriPermission(packageName, outputUri,
                        Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
            }
        }

        startActivityForResult(i, REQUEST_ID);
    }
}

From source file:com.github.vase4kin.teamcityapp.artifact.router.ArtifactRouterImpl.java

/**
 * {@inheritDoc}//from  ww  w .ja v a 2  s .  c om
 */
@Override
public void startFileActivity(File file) {
    MimeTypeMap map = MimeTypeMap.getSingleton();
    String ext = MimeTypeMap.getFileExtensionFromUrl(file.getName());
    String type = map.getMimeTypeFromExtension(ext);

    if (type == null) {
        type = ALL_FILES_TYPE;
    }

    Intent intent = new Intent(Intent.ACTION_VIEW);
    Uri data = FileProvider.getUriForFile(mActivity, BuildConfig.APPLICATION_ID + ".provider", file);
    intent.setDataAndType(data, type);
    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

    //User couldn't have app with type intent
    try {
        mActivity.startActivity(intent);
    } catch (android.content.ActivityNotFoundException e) {
        intent.setDataAndType(data, ALL_FILES_TYPE);
        mActivity.startActivity(intent);
    }
}

From source file:net.bluehack.ui.Components.AvatarUpdater.java

public void openCamera() {
    try {/*www .ja v a  2  s.  co m*/
        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("tmessages", e);
    }
}

From source file:kr.wdream.ui.Components.AvatarUpdater.java

public void openCamera() {
    try {/*  w  ww .  j a  va  2 s.  c  o  m*/
        Log.d("AvatarUpdater", "openCamera");
        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("tmessages", e);
    }
}

From source file:com.digitalarx.android.files.FileOperationsHelper.java

public void openFile(OCFile file) {
    if (file != null) {
        String storagePath = file.getStoragePath();
        String encodedStoragePath = WebdavUtils.encodePath(storagePath);

        Intent intentForSavedMimeType = new Intent(Intent.ACTION_VIEW);
        intentForSavedMimeType.setDataAndType(Uri.parse("file://" + encodedStoragePath), file.getMimetype());
        intentForSavedMimeType// w  w w . java 2s.co  m
                .setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

        Intent intentForGuessedMimeType = null;
        if (storagePath.lastIndexOf('.') >= 0) {
            String guessedMimeType = MimeTypeMap.getSingleton()
                    .getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));
            if (guessedMimeType != null && !guessedMimeType.equals(file.getMimetype())) {
                intentForGuessedMimeType = new Intent(Intent.ACTION_VIEW);
                intentForGuessedMimeType.setDataAndType(Uri.parse("file://" + encodedStoragePath),
                        guessedMimeType);
                intentForGuessedMimeType.setFlags(
                        Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            }
        }

        Intent chooserIntent = null;
        if (intentForGuessedMimeType != null) {
            chooserIntent = Intent.createChooser(intentForGuessedMimeType,
                    mFileActivity.getString(R.string.actionbar_open_with));
        } else {
            chooserIntent = Intent.createChooser(intentForSavedMimeType,
                    mFileActivity.getString(R.string.actionbar_open_with));
        }

        mFileActivity.startActivity(chooserIntent);

    } else {
        Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
    }
}

From source file:com.commonsware.android.tte.DocumentStorageService.java

private void load(Uri document) {
    try {//from  www  . j a  v  a2 s .  c  om
        boolean weHavePermission = false;
        boolean isContent = ContentResolver.SCHEME_CONTENT.equals(document.getScheme());

        if (isContent) {
            int perms = Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION;

            getContentResolver().takePersistableUriPermission(document, perms);

            for (UriPermission perm : getContentResolver().getPersistedUriPermissions()) {
                if (perm.getUri().equals(document)) {
                    weHavePermission = true;
                }
            }
        } else {
            weHavePermission = true;
        }

        if (weHavePermission) {
            try {
                InputStream is = getContentResolver().openInputStream(document);

                try {
                    String text = slurp(is);
                    DocumentFile docFile;

                    if (isContent) {
                        docFile = DocumentFile.fromSingleUri(this, document);
                    } else {
                        docFile = DocumentFile.fromFile(new File(document.getPath()));
                    }

                    EventBus.getDefault().post(
                            new DocumentLoadedEvent(document, text, docFile.getName(), docFile.canWrite()));
                } finally {
                    is.close();
                }
            } catch (Exception e) {
                Log.e(getClass().getSimpleName(), "Exception loading " + document.toString(), e);
                EventBus.getDefault().post(new DocumentLoadErrorEvent(document, e));
            }
        } else {
            Log.e(getClass().getSimpleName(), "We failed to get permissions for " + document.toString());
            EventBus.getDefault().post(new DocumentPermissionFailureEvent(document));
        }
    } catch (SecurityException e) {
        Log.e(getClass().getSimpleName(), "Exception getting permissions for " + document.toString(), e);
        EventBus.getDefault().post(new DocumentPermissionFailureEvent(document));
    }
}

From source file:com.uphyca.kitkat.storage.ui.MainFragment.java

@TargetApi(Build.VERSION_CODES.KITKAT)
@Override/* w w  w.j  a  v  a  2 s  . c o  m*/
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == Activity.RESULT_OK && data.getData() != null) {
        int takeFlags = data.getFlags();
        takeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        // Check for the freshest data.
        getActivity().getContentResolver().takePersistableUriPermission(data.getData(), takeFlags);
    }
    switch (requestCode) {
    case REQUEST_OPEN:
        onOpenRequestResult(resultCode, data);
        return;
    case REQUEST_EDIT:
    case REQUEST_CREATE:
        onEditRequestResult(resultCode, data);
        return;
    case REQUEST_DELETE:
        onDeleteRequestResult(resultCode, data);
        return;
    default:
        super.onActivityResult(requestCode, resultCode, data);
    }
}

From source file:com.xbm.android.matisse.internal.utils.MediaStoreCompat.java

public void dispatchCaptureIntent(Context context, int requestCode) {
    Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (captureIntent.resolveActivity(context.getPackageManager()) != null) {
        File photoFile = null;/*from  w w  w.ja v a2 s .c  o m*/
        try {
            photoFile = createImageFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (photoFile != null) {
            mCurrentPhotoPath = photoFile.getAbsolutePath();
            mCurrentPhotoUri = FileProvider.getUriForFile(mContext.get(), mCaptureStrategy.authority,
                    photoFile);
            captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCurrentPhotoUri);
            captureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                List<ResolveInfo> resInfoList = context.getPackageManager().queryIntentActivities(captureIntent,
                        PackageManager.MATCH_DEFAULT_ONLY);
                for (ResolveInfo resolveInfo : resInfoList) {
                    String packageName = resolveInfo.activityInfo.packageName;
                    context.grantUriPermission(packageName, mCurrentPhotoUri,
                            Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
                }
            }
            if (mFragment != null) {
                mFragment.get().startActivityForResult(captureIntent, requestCode);
            } else {
                mContext.get().startActivityForResult(captureIntent, requestCode);
            }
        }
    }
}