Search code examples
androidimageshare

Share images to another apps


In my app I can get a screen capture and save it to the sd card. I was wondering if it's possible to share it from my app to Whatsapp, for example, as the gallery app does.

In the gallery, you have the chance of "sharing" the file to Whatsapp, Facebook or Twitter.

Can i do the same from my app? How?

Thank you!


Solution

  • Yes, you can.

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @SuppressWarnings( "deprecation" )
    public static Intent shareImage(Context context, String pathToImage) {
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
            shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        else
            shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
    
        shareIntent.setType("image/*");
    
        // For a file in shared storage.  For data in private storage, use a ContentProvider.
        Uri uri = Uri.fromFile(context.getFileStreamPath(pathToImage));
        shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
        return shareIntent;
    }
    

    Here is a detailed link: http://android-developers.blogspot.com/2012/02/share-with-intents.html

    So just add a "share" button and execute startActivity(shareIntent).