Share image Intent

  1. Home
  2. chevron_right
  3. Snippets
  4. chevron_right
  5. Share image Intent

Share image Intent

Whenever you need to share an image from the external storage you can use the startActivity method and an Intent to do that.

Doing so, the user will be able to chose whatever app he likes to share the photo.

Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/png");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///"+imagePath));
startActivity(Intent.createChooser(share, "Share Image"));

If you don’t append the “file:///” prefix to the image path, the apps won’t load your image so that is important too.

And if you need a ready to go method, here you go:

/**
     * Opens a picker dialog displaying all the apps that support image sharing
     *
     * @param context     activity that triggers this method
     * @param image       path to the image that should be uploaded
     * @param dialogTitle the title of the sharing dialog
     */
    public void shareImageIntent(Context context, String image, String dialogTitle) {
        Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("image/png");
        share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///" + image));
        context.startActivity(Intent.createChooser(share, dialogTitle));
    }

 

 

, , ,
Previous Post
Android Gradle & Manifest merge
Next Post
Errors after update to Android Studio 0.8

Related Posts

Menu