This tutorial will help you share image and text on whatsapp from other android application. We can do it by adding filter to intent, where we will provide package name of the application.
In my code I am sharing image stored on drawable folder, you can select image from gallery or use image captured from camera.
1. This is my layout activity_share_image.xml file.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_share_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.customkeyboarddemo.ShareImageActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/selectedImageView"
android:layout_above="@+id/textCaption"
android:src="@drawable/index"/>
<Button
android:text="Share"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:id="@+id/shareButton"
android:layout_alignParentBottom="true"
android:onClick="shareImage"/>
<EditText
android:hint="Enter caption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textCaption"
android:layout_above="@id/shareButton"
android:padding="5dp"/>
</RelativeLayout>
2. This is my main class SelectImageActivity.java.
package com.customkeyboarddemo;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class ShareImageActivity extends AppCompatActivity {
EditText tvCaption;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share_image);
tvCaption = (EditText) findViewById(R.id.textCaption);
}
public void shareImage(View view){
String caption = tvCaption.getText().toString();
//You can select image from gallery too.
Uri selectedImageUri = null;
Bitmap imgBitmap= BitmapFactory.decodeResource(getResources(),R.drawable.index);
String imgBitmapPath= MediaStore.Images.Media.insertImage(getContentResolver(),imgBitmap,"testing",null);
selectedImageUri = Uri.parse(imgBitmapPath);
if(null == selectedImageUri) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, caption);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_STREAM,selectedImageUri);
intent.setType("image/jpeg");
intent.setPackage("com.whatsapp");
startActivity(intent);
}
}
}
Thanks:)
0 Comment(s)