Text and Image Sharing Using Intent – Android Development

0
72

We know that sometimes the applications created by us need to share images and text together. So today we will learn in this blog how to share text and images together on other apps like WhatsApp, Facebook, Twitter, etc.

For more information read here.

Example

Step 1: Create New Project

Step 2: Create an xml layout activity_main.xml for MainActivity.java. This layout has ImageView to display the selected image from the gallery and an EditText for user input and two buttons – the first button to select the image from the gallery and the second button to share the image and text.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="vertical"
    android:padding="16dp">

    <ImageView
        android:id="@+id/imageViewId"
        android:layout_width="200dp"
        android:layout_height="300dp"
        android:layout_gravity="center_horizontal" />

    <EditText
        android:id="@+id/editTextId"
        android:hint="Enter Text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal" />

    <Button
        android:id="@+id/buttonId1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Choose Image"
        android:textColor="#000000"
        android:textSize="18sp" />

    <Button
        android:id="@+id/buttonId2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Share"
        android:textColor="#000000"
        android:textSize="18sp" />
</LinearLayout>

Step 3: In MainActivity.java we have implemented picking the image from gallery and sharing intent.

MainActivity.java

package com.tloaemedia.example;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.EditText; 
import android.widget.ImageView;
import android.widget.Toast;

import java.io.ByteArrayOutputStream;

public class MainActivity extends AppCompatActivity { 
    private Button button1, button2;
    private EditText editText; 
    private ImageView imageView;
    public static final int PICK_IMAGE = 1;
    private Uri uri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); 

        button1 = findViewById(R.id.buttonId1);
        button2 = findViewById(R.id.buttonId2);
        editText = findViewById(R.id.ediTextId); 
        imageView = v.findViewById(R.id.imageViewId);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (uri != null) {
                    Intent shareIntent = new Intent();
                    shareIntent.setAction(Intent.ACTION_SEND);
                    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
                    shareIntent.setType("image/*");
                     shareIntent.putExtra(Intent.EXTRA_TEXT, editText.getText().toString()); 
                    startActivity(Intent.createChooser(shareIntent, "Share Via"));
                } else {
                    Toast.makeText(getActivity(), "Choose Image First", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_IMAGE && resultCode == Activity.RESULT_OK) {
            try {
                uri = data.getData();
                Bitmap imageBitmap = MediaStore.Images.Media.getBitmap(MainActivity.this.getContentResolver(), uri);
                imageView.setImageBitmap(imageBitmap);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

Step 4: Make MainActivity as your default activity in manifests.xml

manifests.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tloaemedia.example">

    <!-- Add these permission in AndroidManifest.xml file -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

Thanks.

LEAVE A REPLY

Please enter your comment!
Please enter your name here