How to Add Images Directly to WhatsApp in Android?
Last Updated :
28 Apr, 2025
An image is a UI widget used to show images or upload images in an Android app. It is the sub-class of the View class. We can use it to add many images, like Bitmaps, and Drawable files. To use it a person can add ImageView to their XML files. Passing its id to the resource file. A sample video is given below to get an idea about what we are going to do in this article.
Step-by-Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:layout_width="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:layout_width="300dp"
android:layout_marginTop="100dp"
android:layout_height="400dp"
android:scaleType="fitXY"
android:id="@+id/imgCamera"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnCamera"
android:layout_marginTop="21dp"
android:backgroundTint="#328736"
android:text="Open Camera"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnGallery"
android:backgroundTint="#328736"
android:layout_marginTop="21dp"
android:text="Open Gallery"/>
</LinearLayout>
activity_main.xml
Step 3: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Java
package com.shruti.cameraexample;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
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.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private final int CAMERA_REQ_CODE = 100;
private final int GALLERY_REQ_CODE = 1000;
ImageView imgCamera;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgCamera=findViewById(R.id.imgCamera);
Button btnCamera = findViewById(R.id.btnCamera);
Button btnGallery = findViewById(R.id.btnGallery);
btnGallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent iGallery = new Intent(Intent.ACTION_PICK);
iGallery.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(iGallery,GALLERY_REQ_CODE);
}
});
btnCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent iCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(iCamera,CAMERA_REQ_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){
if(requestCode==GALLERY_REQ_CODE){
// gallery
imgCamera.setImageURI(data.getData());
} else if (requestCode==CAMERA_REQ_CODE) {
Bitmap img = (Bitmap)(data.getExtras().get("data"));
imgCamera.setImageBitmap(img);
}
}
imgCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) imgCamera.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();
String bitmpath = MediaStore.Images.Media.insertImage(getContentResolver(),bitmap,"upload",null);
Uri uri = Uri.parse(bitmpath);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/png");
shareIntent.setPackage("com.whatsapp");
shareIntent.putExtra(Intent.EXTRA_STREAM,uri);
startActivity(Intent.createChooser(shareIntent,"Share Using"));
}
});
}
}
Step 4:
In your Manifest file add these permissions.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
 Output:
Note: In your app setting allow all the permissions to access your external storage. If you will not do so the app will not work properly.
Similar Reads
How to Add Image on EditText in Android? In Android, An EditText is an overlay over a TextView that makes it editable using text by invoking a soft keyboard in run time. It is generally implemented to collect text data from the user. EditText is most commonly used in forms that require users to fill in details and for passing a search quer
3 min read
How to Add Image to Drawable Folder in Android Studio? The resource folder is the most important folder because it contains all the non-code sources like images, XML layouts, UI strings for the android application. In Android Studio inside the res folder, one can find the drawable folder, layout folder, mipmap folder, values folder, etc. Among them, the
3 min read
How to Add Text Drawable to ImageView in Android? In many android apps, you will get to see a feature in which you can see a simple text is displayed inside an ImageView or you can get to see that a text is displayed in a specific image or a shape. Mostly this type of view is seen in the Contacts application which is present on your Android device.
5 min read
How to send message on WhatsApp in Android Whatsapp is the one of most popular messaging App. Many android applications need the functionality to share some messages directly from their app to WhatsApp. For example, if a user wants to share the app or share a message from the app then this functionality comes in use. Either user can send a t
3 min read
How to Create WhatsApp Stories View in Android? Stories are now becoming one of the most seen features in many different apps such as WhatsApp, LinkedIn, Instagram, and many more. In this article, we will take a look at creating a similar type of view in our Android App. What we are going to build in this article? We will be building a simple a
7 min read