Share Image In Android

In this tutorial, you will learn how to share an image in your Android application. In Android, sharing data with intents is most commonly used for social sharing of content. Share Intent allows users to share content across multiple channels, including email, text messaging, social networking and more. Users are allowed to choose from the list of sharing applications available on their own devices. So lets begin…

Create a new project in Eclipse File > New > Android Application Project. Fill in the details and name your project ShareImageTutorial.

Application Name : ShareImageTutorial

Project Name : ShareImageTutorial

Package Name : com.androidbegin.shareimagetutorial

Open your MainActivity.java and paste the following code.

MainActivity.java

package com.androidbegin.shareimagetutorial;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.widget.ImageView;

public class MainActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Get the view from activity_main.xml
		setContentView(R.layout.activity_main);

		// Create an actionbar
		ActionBar actionBar = getActionBar();
		actionBar.show();

		// Locate ImageView in activity_main.xml
		ImageView myimage = (ImageView) findViewById(R.id.image);

		// Attach image into ImageView
		myimage.setImageResource(R.drawable.wallpaper);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Create an actionbar menu
		menu.add("Share Image")
				// Add a new Menu Button
				.setOnMenuItemClickListener(this.ShareImageClickListener)
				.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

		return super.onCreateOptionsMenu(menu);
	}

	// Capture actionbar menu item click
	OnMenuItemClickListener ShareImageClickListener = new OnMenuItemClickListener() {

		public boolean onMenuItemClick(MenuItem item) {

			Bitmap bitmap;
			OutputStream output;

			// Retrieve the image from the res folder
			bitmap = BitmapFactory.decodeResource(getResources(),
					R.drawable.wallpaper);

			// Find the SD Card path
			File filepath = Environment.getExternalStorageDirectory();

			// Create a new folder AndroidBegin in SD Card
			File dir = new File(filepath.getAbsolutePath() + "/Share Image Tutorial/");
			dir.mkdirs();

			// Create a name for the saved image
			File file = new File(dir, "sample_wallpaper.png");

			try {

				// Share Intent
				Intent share = new Intent(Intent.ACTION_SEND);

				// Type of file to share
				share.setType("image/jpeg");

				output = new FileOutputStream(file);

				// Compress into png format image from 0% - 100%
				bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
				output.flush();
				output.close();

				// Locate the image to Share
				Uri uri = Uri.fromFile(file);

				// Pass the image into an Intnet
				share.putExtra(Intent.EXTRA_STREAM, uri);

				// Show the social share chooser list
				startActivity(Intent.createChooser(share, "Share Image Tutorial"));

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return false;
		}
	};
}

We have created an ImageView to show the attached image on the graphical layout and an actionbar menu. On actionbar menu item click will show a chooser list. The chooser list allows users to choose from the list of sharing applications available on their own devices.

We have prepared a sample image for this tutorial. Insert your downloaded sample image into your res > drawable-hdpi.

Sample Image

[wpfilebase tag=file id=78 tpl=download-button /]

Next, create an XML file for your MainActivity Graphical Layout. Go to res > layout > Right Click on layout > New >Android XML File

Name your new XML file activity_main.xml and paste the following code.

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

Next, change the application name. Open your strings.xml in your res > values folder and paste the following code.

strings.xml

<resources>

    <string name="app_name">Share Image Tutorial</string>
    <string name="menu_settings">Settings</string>

</resources>

In your AndroidManifest.xml, we need to declare a permission to allow the application to write external storage. Open your AndroidManifest.xml and paste the following code.

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidbegin.shareimagetutorial"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:uiOptions="splitActionBarWhenNarrow" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Output:

Share Image Tutorial ScreenShots

Source Code 

[purchase_link id=”7833″ text=”Purchase to Download Source Code” style=”button” color=”green”]

Latest comments

THIS CODE DOESN'T WORKS AT ALL...UNFORTUNATELY THE APP HAS STOPPED APPEARS WHEN I DEBUG IT

Shagun Shah

Share Image In Android

how to share image from one app to another app i am tring this code but on run time it causes a fatal error help me how to resolve it sir.

Gourav Singla

Share Image In Android

Good post only one question, how to share image from url ?

matiasgq

Share Image In Android

09-23 21:48:00.410: E/AndroidRuntime(4409): java.lang.NoSuchMethodError: com.androidbegin.shareimagetutorial.MainActivity.getActionBar

Rishil Bhatt

Share Image In Android