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.duy.ascii.sharedcode.ShareUtil.java

public static void shareImage(Context context, File file) {
    Uri uri;// w w w.j  a v a 2  s. co m
    if (Build.VERSION.SDK_INT >= 24) {
        uri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file);
    } else {
        uri = Uri.fromFile(file);
    }
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_STREAM, uri);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.setType("image/*");
    context.startActivity(Intent.createChooser(intent, "Share image via"));
}

From source file:Main.java

/**
 * Helper method for sharing an image.// w w w  . j  a v a 2  s  . c  o  m
 *
 * @param context   The image context.
 * @param imagePath The path of the image to be shared.
 */
static void shareImage(Context context, String imagePath) {
    // Create the share intent and start the share activity
    File imageFile = new File(imagePath);
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("image/*");
    Uri photoURI = FileProvider.getUriForFile(context, FILE_PROVIDER_AUTHORITY, imageFile);
    shareIntent.putExtra(Intent.EXTRA_STREAM, photoURI);
    context.startActivity(shareIntent);
}

From source file:org.chromium.chrome.browser.FileProviderHelper.java

@Override
public Uri getContentUriFromFile(Context context, File file) {
    return FileProvider.getUriForFile(context, context.getPackageName() + API_AUTHORITY_SUFFIX, file);
}

From source file:com.android.contacts.util.ContactPhotoUtils.java

/**
 * Generate a new, unique file to be used as an out-of-band communication
 * channel, since hi-res Bitmaps are too big to serialize into a Bundle.
 * This file will be passed (as a uri) to other activities (such as the gallery/camera/
 *  cropper/etc.), and read by us once they are finished writing it.
 *//*  w ww .  j ava 2 s  .c om*/
public static Uri generateTempImageUri(Context context) {
    final String fileProviderAuthority = context.getResources()
            .getString(R.string.photo_file_provider_authority);
    return FileProvider.getUriForFile(context, fileProviderAuthority,
            new File(pathForTempPhoto(context, generateTempPhotoFileName())));
}

From source file:com.example.linhdq.test.main_menu.ContactActivity.java

public static Intent getFeedbackIntent(Context context, String subject, File file, String body) {
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("message/rfc822");
    if (subject != null) {
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    }/*  w w  w.ja  v  a 2s. c  o m*/
    if (file.exists()) {
        final Uri uriForFile = FileProvider.getUriForFile(context,
                context.getString(R.string.config_share_file_auth), file);
        intent.putExtra(Intent.EXTRA_STREAM, uriForFile);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    }
    if (body != null) {
        intent.putExtra(Intent.EXTRA_TEXT, body);
    }

    intent.putExtra(Intent.EXTRA_EMAIL, new String[] { FEEDBACK_MAIL });
    return intent;
}

From source file:com.renard.ocr.main_menu.ContactActivity.java

public static Intent getFeedbackIntent(Context context, String subject, File file, String body) {
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("message/rfc822");
    if (subject != null) {
        intent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
    }/*from w  ww .ja va  2 s.c o m*/
    if (file.exists()) {
        final Uri uriForFile = FileProvider.getUriForFile(context,
                context.getString(R.string.config_share_file_auth), file);
        intent.putExtra(Intent.EXTRA_STREAM, uriForFile);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    }
    if (body != null) {
        intent.putExtra(Intent.EXTRA_TEXT, body);
    }

    intent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { FEEDBACK_MAIL });
    return intent;
}

From source file:com.github.stkent.bugshaker.flow.email.screenshot.ScreenshotUriObservable.java

static Observable<Uri> create(@NonNull final Context applicationContext, @NonNull final Bitmap bitmap,
        @NonNull final Logger logger) {

    return Observable.create(new Observable.OnSubscribe<Uri>() {
        @Override/*from  w ww  .  ja  v a  2 s.c o m*/
        public void call(final Subscriber<? super Uri> subscriber) {
            OutputStream fileOutputStream = null;

            try {
                final File screenshotFile = ScreenshotUtil.getScreenshotFile(applicationContext);

                fileOutputStream = new BufferedOutputStream(new FileOutputStream(screenshotFile));

                bitmap.compress(Bitmap.CompressFormat.JPEG, JPEG_COMPRESSION_QUALITY, fileOutputStream);

                fileOutputStream.flush();

                logger.d("Screenshot saved to " + screenshotFile.getAbsolutePath());

                final Uri result = FileProvider.getUriForFile(applicationContext,
                        applicationContext.getPackageName() + AUTHORITY_SUFFIX, screenshotFile);

                logger.d("Screenshot Uri created: " + result);

                subscriber.onNext(result);
                subscriber.onCompleted();
            } catch (final IOException e) {
                subscriber.onError(e);
            } finally {
                if (fileOutputStream != null) {
                    try {
                        fileOutputStream.close();
                    } catch (final IOException ignored) {
                        // We did our best...
                        logger.e("Failed to close OutputStream.");
                    }
                }
            }
        }
    });
}

From source file:ca.rmen.android.scrumchatter.export.Export.java

/**
 * Bring up the chooser to send the file.
 *//*  w w  w. j  av a  2  s . com*/
static void share(Context context, File file, String mimeType) {
    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_SUBJECT, context.getString(R.string.export_message_subject));
    sendIntent.putExtra(Intent.EXTRA_TEXT, context.getString(R.string.export_message_body));
    Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileprovider", file);
    sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
    sendIntent.setType(mimeType);
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
        List<ResolveInfo> resInfoList = context.getPackageManager().queryIntentActivities(sendIntent,
                PackageManager.MATCH_DEFAULT_ONLY);
        for (ResolveInfo resolveInfo : resInfoList) {
            String packageName = resolveInfo.activityInfo.packageName;
            context.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Log.v(TAG, "grant permission to " + packageName);
        }
    }
    context.startActivity(
            Intent.createChooser(sendIntent, context.getResources().getText(R.string.action_share)));
}

From source file:io.github.protino.codewatch.utils.FileProviderUtils.java

public static void shareBitmap(Context context, Bitmap bitmap) throws IOException {
    final String imageName = "/" + String.valueOf(System.currentTimeMillis());

    //First save the bitmap to cache directory
    File directory = new File(context.getCacheDir(), CACHE_DIR_NAME);
    if (!directory.mkdirs()) {
        //delete all data under this folder
        deleteRecursive(directory);//w ww  .ja  v a2  s  . c  om
        //recreate the directory
        directory = new File(context.getCacheDir(), CACHE_DIR_NAME);
        directory.mkdirs();
    }
    FileOutputStream stream = new FileOutputStream(directory + imageName + IMAGE_EXTENSION);
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    stream.flush();
    stream.close();

    //Now create uri through FileProvider
    File newFile = new File(directory, imageName + IMAGE_EXTENSION);
    Uri uri = FileProvider.getUriForFile(context, FILE_PROVIDER_AUTHORITY, newFile);

    if (uri != null) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.setDataAndType(uri, context.getContentResolver().getType(uri));
        intent.setType("image/png");
        intent.putExtra(Intent.EXTRA_STREAM, uri);
        context.startActivity(Intent.createChooser(intent, context.getString(R.string.choose_app)));
    }
}

From source file:de.baumann.quitsmoking.helper.Popup_camera.java

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

    Toast.makeText(Popup_camera.this, R.string.cam_start, Toast.LENGTH_LONG).show();

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File f = helper_main.newFile();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        Uri contentUri = FileProvider.getUriForFile(Popup_camera.this,
                getApplicationContext().getPackageName() + ".provider", f);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);
    } else {//from   www .j  a va2s. c  o m
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
    }

    try {
        startActivityForResult(intent, 1);
        finish();
    } catch (ActivityNotFoundException e) {
        Toast.makeText(Popup_camera.this, R.string.cam_fail, Toast.LENGTH_SHORT).show();
    }
}