Example usage for android.support.v4.content FileProvider getUriForFile

List of usage examples for android.support.v4.content FileProvider getUriForFile

Introduction

In this page you can find the example usage for android.support.v4.content FileProvider getUriForFile.

Prototype

public static Uri getUriForFile(Context context, String str, File file) 

Source Link

Usage

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 a 2  s  .co 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);
            }
        }
    }
}

From source file:org.goodev.material.util.ShareDribbbleImageTask.java

@Override
protected void onPostExecute(File result) {
    if (result == null) {
        return;/*from   ww w .j  a v a  2  s . c om*/
    }
    // glide cache uses an unfriendly & extension-less name,
    // massage it based on the original
    //        result.renameTo(renamed);
    Uri uri = FileProvider.getUriForFile(activity, activity.getString(R.string.share_authority), result);
    ShareCompat.IntentBuilder.from(activity).setText(getShareText()).setType(getImageMimeType(result.getName()))
            .setSubject(shot.title).setStream(uri).startChooser();
}

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

public static Uri generateTempCroppedImageUri(Context context) {
    return FileProvider.getUriForFile(context, FILE_PROVIDER_AUTHORITY,
            new File(pathForTempPhoto(context, generateTempCroppedPhotoFileName())));
}

From source file:at.ac.tuwien.caa.docscan.logic.DataLog.java

public void shareLog(Activity activity) {

    File logPath = new File(activity.getBaseContext().getFilesDir(), LOG_FILE_NAME);
    Uri contentUri = FileProvider.getUriForFile(activity.getBaseContext(), "at.ac.tuwien.caa.fileprovider",
            logPath);//from   w  w w.j  a v a  2 s .  c o  m

    String emailSubject = activity.getBaseContext().getString(R.string.log_email_subject);
    String[] emailTo = new String[] { activity.getBaseContext().getString(R.string.log_email_to) };
    String text = activity.getBaseContext().getString(R.string.log_email_text);

    Intent intent = ShareCompat.IntentBuilder.from(activity).setType("text/plain").setSubject(emailSubject)
            .setEmailTo(emailTo).setStream(contentUri).setText(text).getIntent()
            .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

    activity.startActivity(intent);

}

From source file:com.github.czy1121.update.app.utils.UpdateUtil.java

public static void install(Context context, File file, boolean force) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
        intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
    } else {// ww  w  . j  a  v  a 2  s . c o m
        Uri uri = FileProvider.getUriForFile(context, context.getPackageName() + ".updatefileprovider", file);
        intent.setDataAndType(uri, "application/vnd.android.package-archive");
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    }
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
    if (force) {
        System.exit(0);
    }
}

From source file:com.danimahardhika.android.helpers.core.FileHelper.java

@Nullable
public static Uri getUriFromFile(@NonNull Context context, String applicationId, @NonNull File file) {
    try {//  w w w  . j  a  v a2s  . c om
        return FileProvider.getUriForFile(context, applicationId + ".fileProvider", file);
    } catch (IllegalArgumentException ignored) {
    }
    return null;
}

From source file:com.linkedin.android.shaky.Utils.java

/**
 * Get the file provider Uri, so that internal files can be temporarily shared with other apps.
 *
 * Requires AndroidManifest permission: android.support.v4.content.FileProvider
 *//* w  ww . j a  v a  2  s. c  om*/
@NonNull
static Uri getProviderUri(@NonNull Context context, @NonNull File file) {
    String authority = context.getPackageName() + FILE_PROVIDER_SUFFIX;
    return FileProvider.getUriForFile(context, authority, file);
}

From source file:org.mozilla.focus.broadcastreceiver.DownloadBroadcastReceiver.java

private void displaySnackbar(final Context context, long completedDownloadReference,
        DownloadManager downloadManager) {
    if (!isFocusDownload(completedDownloadReference)) {
        return;/*from ww w  .j a  v  a  2  s . c om*/
    }

    final DownloadManager.Query query = new DownloadManager.Query();
    query.setFilterById(completedDownloadReference);
    try (Cursor cursor = downloadManager.query(query)) {
        if (cursor.moveToFirst()) {
            int statusColumnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
            if (DownloadManager.STATUS_SUCCESSFUL == cursor.getInt(statusColumnIndex)) {
                String uriString = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));

                final String localUri = uriString.startsWith(FILE_SCHEME)
                        ? uriString.substring(FILE_SCHEME.length())
                        : uriString;
                final String fileExtension = MimeTypeMap.getFileExtensionFromUrl(localUri);
                final String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);
                final String fileName = URLUtil.guessFileName(Uri.decode(localUri), null, mimeType);

                final File file = new File(Uri.decode(localUri));
                final Uri uriForFile = FileProvider.getUriForFile(context,
                        BuildConfig.APPLICATION_ID + FILE_PROVIDER_EXTENSION, file);
                final Intent openFileIntent = IntentUtils.createOpenFileIntent(uriForFile, mimeType);
                showSnackbarForFilename(openFileIntent, context, fileName);
            }
        }
    }
    removeFromHashSet(completedDownloadReference);
}

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

public void showAlert(final boolean fromTheme) {
    AlertDialog.Builder builder = new AlertDialog.Builder(parentActivity);
    CharSequence[] items;//from w w w . j  a v  a  2s.  c o m
    if (fromTheme) {
        items = new CharSequence[] { LocaleController.getString("FromCamera", R.string.FromCamera),
                LocaleController.getString("FromGalley", R.string.FromGalley),
                LocaleController.getString("SelectColor", R.string.SelectColor),
                LocaleController.getString("Default", R.string.Default),
                LocaleController.getString("Cancel", R.string.Cancel) };
    } else {
        items = new CharSequence[] { LocaleController.getString("FromCamera", R.string.FromCamera),
                LocaleController.getString("FromGalley", R.string.FromGalley),
                LocaleController.getString("Cancel", R.string.Cancel) };
    }
    builder.setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            try {
                if (i == 0) {
                    try {
                        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(
                                        parentActivity, 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();
                        }
                        parentActivity.startActivityForResult(takePictureIntent, 10);
                    } catch (Exception e) {
                        FileLog.e(e);
                    }
                } else if (i == 1) {
                    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
                    photoPickerIntent.setType("image/*");
                    parentActivity.startActivityForResult(photoPickerIntent, 11);
                } else if (fromTheme) {
                    if (i == 2) {
                        delegate.needOpenColorPicker();
                    } else if (i == 3) {
                        delegate.didSelectWallpaper(null, null);
                    }
                }
            } catch (Exception e) {
                FileLog.e(e);
            }
        }
    });
    builder.show();
}

From source file:com.ultramegasoft.flavordex2.util.PhotoUtils.java

/**
 * Get an Intent to capture a photo./*from   www  .j a  v  a 2  s . co m*/
 *
 * @return Image capture Intent
 */
@Nullable
public static Intent getTakePhotoIntent(@NonNull Context context) {
    try {
        final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        final Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileprovider",
                getOutputMediaFile());
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            intent.setClipData(ClipData.newUri(context.getContentResolver(), null, uri));
            intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        } else {
            final List<ResolveInfo> activities = context.getPackageManager().queryIntentActivities(intent,
                    PackageManager.MATCH_DEFAULT_ONLY);
            for (ResolveInfo activity : activities) {
                final String name = activity.activityInfo.packageName;
                context.grantUriPermission(name, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            }
        }

        return intent;
    } catch (IOException e) {
        Log.e(TAG, "Failed to create new file", e);
    }
    return null;
}