This tutorial shows you the process of implementing the share Intent. The share Intent allows users of your apps to share content across multiple channels, including email, text messaging, social networking and more. In this tutorial, I have implemented share Intent in the android grid view example.
The share Intent looks like:

Share Intent
Creating a share Intent
The following snippet creates a share Intent.
|
1 2 3 4 5 6 |
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND); /*sets the sharing type */ shareIntent.setType("image/png"); //image shareIntent.setType("text/plain"); //text |
The following snippet creates a share Intent for plain text which can be used in mail or messaging.
|
1 2 3 4 5 6 |
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND); shareIntent.setType("text/plain"); String shareBody = "This is the body to be shared"; sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject of the text"); sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody); startActivity(Intent.createChooser(sharingIntent, "Share via")); |
The last line creates a Chooser which lets user choose their sharing medium.

Share Intent for plain text
Share Intent for the image in the full image view of the grid view example
Replace the previous FullImage.java file with the following code.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
package com.cw.grid; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import com.cw.grid.GridViewActivity.ImageAdapter; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class FullImage extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fullimage); Intent intent = getIntent(); int position = intent.getExtras().getInt("id"); GridViewActivity x = new GridViewActivity(); ImageAdapter imageAdapter = x.new ImageAdapter(this); final ImageView imageView = (ImageView) findViewById(R.id.image_View); imageView.setImageResource(imageAdapter.mThumbIds[position]); Button shareButton = (Button) findViewById(R.id.button1); shareButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { shareIt(); } private Intent shareIt() { imageView.setDrawingCacheEnabled(true); Bitmap icon = imageView.getDrawingCache(); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes); File file = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg"); try { file.createNewFile(); FileOutputStream fo = new FileOutputStream(file); fo.write(bytes.toByteArray()); } catch (IOException e) { e.printStackTrace(); } Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND); shareIntent.setType("image/png"); shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); Uri uri = Uri.parse("file:///sdcard/temporary_file.jpg"); shareIntent.putExtra(Intent.EXTRA_STREAM, uri); startActivity(Intent.createChooser(shareIntent, "Share via")); return shareIntent; } }); } } |
Now on running the project, you can see a share button in the full image view, clicking which you get options to share the image.







