My Life with Android
Introduction Android Componets UserInterface Advanced UI Data Storage Advanced Concepts Others New Studio

Share Image via Intent

In AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
In activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ic_launcher" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="61dp"
        android:text="Share" />

</LinearLayout>
In MainActivity.java
package com.example.androidcollegeppt;

import java.io.ByteArrayOutputStream;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class SocialMediaShareEx extends Activity{
	private ImageView img;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.socialmedia_share);
		img=(ImageView)findViewById(R.id.imageView);
		Button b1=(Button)findViewById(R.id.button);
		b1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				Bitmap b =BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
				Intent share = new Intent(Intent.ACTION_SEND);
				share.setType("image/jpeg");
				ByteArrayOutputStream bytes = new ByteArrayOutputStream();
				b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
				String path = MediaStore.Images.Media.insertImage(getContentResolver(), b, "title", null);
				Uri imageUri = Uri.parse(path);
				share.putExtra(Intent.EXTRA_TITLE, "Fans App");
				share.putExtra(Intent.EXTRA_TEXT, "Hello,This is myapp : https://play.google.com/store/apps/details?id=com.kiloo.subwaysurf");
				share.putExtra(Intent.EXTRA_STREAM, imageUri);
				startActivity(Intent.createChooser(share, "Select"));
			}
		});
	}

}