Android provide us an Intent by which we can use to share image, video, audio, text, etc. to other installed apps like WhatsApp, Facebook, Twitter, etc. We had seen many apps has functionality of sharing image and text to other app and also they have functionality to make other apps share their text and image to the app like WhatsApp from which we can share text and image and also we can share text and image to WhatsApp.
For more information about this Sharing Intent you can read over here.
Click here to create your best canvas prints.
Today we are going to create an app that share text and image to other app and also the other app can share their image and text to this app.
In this tutorial, we are going to learn how to share image and text to other apps and help other apps to share image and text to our app.
1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. By default my activity is ShareActivity.java.
2. Open res ⇒ values ⇒ strings.xml and add below string values. These are some strings that we are going to use in our project.
1 2 3 4 5 6 7 8 9 10 11 12 |
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="share_activity">Share Activity Demo</string> <string name="shared_activity">Shared Activity Demo</string> <string name="imageview">Share ImageView</string> <string name="share_image">Share Image</string> <string name="share_text">Share Text</string> <string name="select_image">Select Image</string> <string name="enter_text">Enter something to share</string> <string name="shared_text">Shared Text</string> <string name="shared_image">Shared Image</string> </resources> |
3. Now, create an xml layout naming share_activity.xml for ShareActivity.java. In this layout i had taken ImageView to display the selected image from gallery and share button for image and one edittext for user input to share the text.
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:fillViewport="true" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" android:padding="5dp" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="5dp" android:text="@string/share_image" android:textColor="#ff6600" android:textSize="25sp" android:textStyle="bold" /> <ImageView android:id="@+id/share_imageview" android:layout_width="200dp" android:layout_height="200dp" android:layout_gravity="center" android:layout_marginTop="10dp" android:contentDescription="@string/imageview" android:padding="5dp" android:src="@drawable/logo" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="5dp" > <Button android:id="@+id/select_image" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="3dp" android:layout_weight="1" android:padding="5dp" android:text="@string/select_image" /> <Button android:id="@+id/share_image" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="3dp" android:layout_weight="1" android:padding="5dp" android:text="@string/share_image" android:visibility="gone" /> </LinearLayout> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:gravity="center" android:padding="5dp" android:text="@string/share_text" android:textColor="#ff6600" android:textSize="25sp" android:textStyle="bold" /> <EditText android:id="@+id/text_share" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:hint="@string/enter_text" android:inputType="text" android:padding="10dp" android:textColor="#000000" android:textColorHint="#d3d3d3" android:textSize="15sp" /> <Button android:id="@+id/btnTextShare" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:padding="5dp" android:text="@string/share_text" /> </LinearLayout> </ScrollView> |
4. For sharing image we have to follow some steps :
For sharing text also we have to follow some steps :
5. Finally come to your ShareActivity.java and add the following code. In this code i had also implemented to choose an image from gallery for image sharing. If you don’t know how to pick an image from gallery you can read my earlier tutorial.
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
package com.imagetext_share_demo; import java.io.FileNotFoundException; import com.imagetext_share_demo.R; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; public class ShareActivity extends AppCompatActivity implements OnClickListener { private static Button selectImage, shareImage, shareText; private static ImageView imageView; private static EditText textToShare; // Uri for image path private static Uri imageUri = null; private final int select_photo = 1; // request code fot gallery intent @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.share_activity); init(); setListeners(); } // Initialize views private void init() { selectImage = (Button) findViewById(R.id.select_image); shareImage = (Button) findViewById(R.id.share_image); shareText = (Button) findViewById(R.id.btnTextShare); imageView = (ImageView) findViewById(R.id.share_imageview); textToShare = (EditText) findViewById(R.id.text_share); } // Implement click listeners private void setListeners() { selectImage.setOnClickListener(this); shareImage.setOnClickListener(this); shareText.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.select_image: // Intent to gallery Intent in = new Intent(Intent.ACTION_PICK); in.setType("image/*"); startActivityForResult(in, select_photo);// start // activity // for // result break; case R.id.share_image: // share image shareImage(imageUri); break; case R.id.btnTextShare: // Share text String getText = textToShare.getText().toString(); if (!getText.equals("") && getText.length() != 0) shareText(getText); else Toast.makeText(ShareActivity.this, "Please enter something to share.", Toast.LENGTH_SHORT) .show(); break; } } protected void onActivityResult(int requestcode, int resultcode, Intent imagereturnintent) { super.onActivityResult(requestcode, resultcode, imagereturnintent); switch (requestcode) { case select_photo: if (resultcode == RESULT_OK) { try { imageUri = imagereturnintent.getData();// Get intent // data Bitmap bitmap = Utils.decodeUri(ShareActivity.this, imageUri, 200);// call // deocde // uri // method // Check if bitmap is not null then set image else show // toast if (bitmap != null) { imageView.setImageBitmap(bitmap);// Set image over // bitmap shareImage.setVisibility(View.VISIBLE);// Visible button // if bitmap is // not null } else { shareImage.setVisibility(View.GONE); Toast.makeText(ShareActivity.this, "Error while decoding image.", Toast.LENGTH_SHORT).show(); } } catch (FileNotFoundException e) { e.printStackTrace(); Toast.makeText(ShareActivity.this, "File not found.", Toast.LENGTH_SHORT).show(); } } } } // Share image private void shareImage(Uri imagePath) { Intent sharingIntent = new Intent(Intent.ACTION_SEND); sharingIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); sharingIntent.setType("image/*"); sharingIntent.putExtra(Intent.EXTRA_STREAM, imagePath); startActivity(Intent.createChooser(sharingIntent, "Share Image Using")); } // Share text private void shareText(String text) { Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); sharingIntent.setType("text/plain");// Plain format text // You can add subject also /* * sharingIntent.putExtra( android.content.Intent.EXTRA_SUBJECT, * "Subject Here"); */ sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, text); startActivity(Intent.createChooser(sharingIntent, "Share Text Using")); } } |
6. Create a new class naming Utils.java for decoding large image into smaller image while picking an image from gallery.
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 |
package com.imagetext_share_demo; import java.io.FileNotFoundException; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; public class Utils { // Method that deocde uri into bitmap. This method is necessary to deocde // large size images to load over imageview public static Bitmap decodeUri(Context context, Uri uri, final int requiredSize) throws FileNotFoundException { BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; BitmapFactory.decodeStream(context.getContentResolver() .openInputStream(uri), null, o); int width_tmp = o.outWidth, height_tmp = o.outHeight; int scale = 1; while (true) { if (width_tmp / 2 < requiredSize || height_tmp / 2 < requiredSize) break; width_tmp /= 2; height_tmp /= 2; scale *= 2; } BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; return BitmapFactory.decodeStream(context.getContentResolver() .openInputStream(uri), null, o2); } } |
Still we had learned how to share an image and text to other apps. Now we are going to learn how to make our app in such a way so that other apps can also share image and text.
7. For other apps to share image and text we have to create a layout to display image and text. I named this layout shared_activity.xml.
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 |
<LinearLayout 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" android:gravity="center" android:orientation="vertical" android:padding="5dp" > <TextView android:id="@+id/shared_textview_title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:gravity="center" android:padding="5dp" android:text="@string/shared_text" android:textColor="#ff6600" android:textSize="25sp" android:textStyle="bold" android:visibility="gone" /> <TextView android:id="@+id/shared_textview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:padding="5dp" android:textColor="#000000" android:textSize="15sp" android:visibility="gone" /> <TextView android:id="@+id/shared_imageview_title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center" android:padding="5dp" android:text="@string/shared_image" android:textColor="#ff6600" android:textSize="25sp" android:textStyle="bold" android:visibility="gone" /> <ImageView android:id="@+id/shared_imageview" android:layout_width="200dp" android:layout_height="200dp" android:layout_gravity="center" android:layout_marginTop="5dp" android:contentDescription="@string/imageview" android:padding="5dp" android:src="@drawable/logo" android:visibility="gone" /> </LinearLayout> |
8. Now create a new class naming SharedActivity.java for sharing text and image to this activity. In this activity we get the intent action and action type and check the conditions according to text and image and display according to it.
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
package com.imagetext_share_demo; import java.io.FileNotFoundException; import com.imagetext_share_demo.R; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.ImageView; import android.widget.TextView; public class SharedActivity extends AppCompatActivity { private static ImageView sharedImageView; private static TextView sharedTextView, shared_textview_title, shared_imageview_title; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.shared_activity); sharedImageView = (ImageView) findViewById(R.id.shared_imageview); sharedTextView = (TextView) findViewById(R.id.shared_textview); shared_textview_title = (TextView) findViewById(R.id.shared_textview_title); shared_imageview_title = (TextView) findViewById(R.id.shared_imageview_title); // Call shared intent onSharedIntent(); } // Shared intent method private void onSharedIntent() { Intent receiverdIntent = getIntent();// Receive intent String receivedAction = receiverdIntent.getAction();// Get Action from // receive intent String receivedType = receiverdIntent.getType();// Get type from receive // intent // If action is equal to action send if (receivedAction.equals(Intent.ACTION_SEND)) { // If nothing shared sharedTextView.setText("Nothing has shared"); sharedTextView.setVisibility(View.VISIBLE); shared_textview_title.setVisibility(View.GONE); sharedImageView.setVisibility(View.GONE); shared_imageview_title.setVisibility(View.GONE); // If mime type is equal to text then show text and hide imageview if (receivedType.startsWith("text/")) { sharedImageView.setVisibility(View.GONE); shared_imageview_title.setVisibility(View.GONE); String receivedText = receiverdIntent .getStringExtra(Intent.EXTRA_TEXT); if (receivedText != null) { sharedTextView.setText(receivedText); sharedTextView.setVisibility(View.VISIBLE); shared_textview_title.setVisibility(View.VISIBLE); } } // If mime type is equal to image then show image and hide textview else if (receivedType.startsWith("image/")) { sharedTextView.setVisibility(View.GONE); shared_textview_title.setVisibility(View.GONE); Uri receiveUri = (Uri) receiverdIntent .getParcelableExtra(Intent.EXTRA_STREAM); if (receiveUri != null) { try { Bitmap bitmap = Utils.decodeUri(SharedActivity.this, receiveUri, 200); sharedImageView.setImageBitmap(bitmap); sharedImageView.setVisibility(View.VISIBLE); shared_imageview_title.setVisibility(View.VISIBLE); } catch (FileNotFoundException e) { e.printStackTrace(); } } } } else if (receivedAction.equals(Intent.ACTION_MAIN)) { sharedTextView.setText("Nothing has shared"); sharedTextView.setVisibility(View.VISIBLE); shared_textview_title.setVisibility(View.GONE); sharedImageView.setVisibility(View.GONE); shared_imageview_title.setVisibility(View.GONE); } } } |
9. Finally the main thing is to add this SharedActivity.java to Manifest file so that your activity should list in Sharing listview. For this you have to add some intent filter and data type that activity gonna listen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<activity android:name="com.imagetext_share_demo.SharedActivity" android:icon="@drawable/share_this" android:label="@string/shared_activity" > <intent-filter> <action android:name="android.intent.action.SEND" /> <!-- Send action required to display activity in share list --> <category android:name="android.intent.category.DEFAULT" /> <!-- Make activity default to launch --> <!-- Mime type i.e. what can be shared with this activity only image and text --> <data android:mimeType="image/*" /> <data android:mimeType="text/*" /> </intent-filter> </activity> |
Your Manifest will look like this :
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 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.imagetext_share_demo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="22" /> <application android:allowBackup="true" android:icon="@drawable/share" android:label="@string/share_activity" android:theme="@style/AppTheme" > <activity android:name="com.imagetext_share_demo.ShareActivity" android:icon="@drawable/share" android:label="@string/share_activity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- Shared Activity --> <activity android:name="com.imagetext_share_demo.SharedActivity" android:icon="@drawable/share_this" android:label="@string/shared_activity" > <intent-filter> <action android:name="android.intent.action.SEND" /> <!-- Send action required to display activity in share list --> <category android:name="android.intent.category.DEFAULT" /> <!-- Make activity default to launch --> <!-- Mime type i.e. what can be shared with this activity only image and text --> <data android:mimeType="image/*" /> <data android:mimeType="text/*" /> </intent-filter> </activity> </application> </manifest> |
10. Now, you are all done, run your app and you will get the output as shown in video.
Thanks.
Subscribe to us and get the latest news.