Wednesday, December 21, 2011

'Tis the season for sharing (via your Android code)

Come Facebook, come Twitter, come Tumblr and Flickr!

No app is an island, we often want to be able to share the cool things we create with our apps with the world, whether it's the latest social network to on-sell our personal details, or just plain old email to your gran.

.. And here's how.

Lets say we have an image file located here:
String fileLocation = "/mnt/sdcard/SomeMediaFiles/aMediaFile10.jpg"

We can share this easily by passing the reference to our file location into the below method:
    public void shareFile(String newFilePath) {
      {Intent share = new Intent(Intent.ACTION_SEND);
//declare the MIME type of the file. .. What you pass into share.setType determines what apps appears in your Sharing list.

      Uri imageUri = Uri.fromFile(new File(newImagePath));
      share.putExtra(Intent.EXTRA_STREAM, imageUri);
      startActivity(Intent.createChooser(share, "Share"));
And this is basically what is the result (actual apps listed will depend on what is installed on your users device):

... Easy.

1 comment:

  1. Cool post!
    Heres my attempt at a simple hello world app for android!