How to Share Image of Your App with Another App in Android?
Last Updated :
23 Nov, 2022
Most of the time while using an app we want to share images from the app to another app. While using Many Social Media Platforms we find this feature to be very useful when we want to share information from one app to another. The Android Intent resolver is used when sending data to another app as part of a well-defined task flow. To use the Android intent resolver, create an intent and add extras. Here we are going to understand how to do that.
Note: If you want to share the Text of Your App with Another App in Android then please refer to this .
Step by Step Implementation
Step 1: Create a new Project
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 AndroidManifest.xml file
Add the following permission to the AndroidManifest.xml file
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Add the following lines inside the <application> tag and after the </activity> tag.
XML
<provider
android:name= "androidx.core.content.FileProvider"
<!-- Write down your package name -- >
android:authorities="com.anni.shareimage.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<!-- we intend to request content
URIs for the images/subdirectory
of your private file area -->
<meta-data
android:name= "android.support.FILE_PROVIDER_PATHS"
android:resource= "@xml/paths" />
</provider>
Below is the complete code for the AndroidManifest.xml file
XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android= "http://schemas.android.com/apk/res/android"
package= "com.anni.shareimage" >
<uses-permission android:name= "android.permission.READ_EXTERNAL_STORAGE" />
<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= ".ViewPdfActivity" ></activity>
<activity android:name= ".MainActivity" >
<intent-filter>
<action android:name= "android.intent.action.MAIN" />
<category android:name= "android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name= "androidx.core.content.FileProvider"
android:authorities= "com.anni.shareimage.fileprovider"
android:exported= "false"
android:grantUriPermissions= "true" >
<meta-data
android:name= "android.support.FILE_PROVIDER_PATHS"
android:resource= "@xml/paths" />
</provider>
</application>
</manifest>
Step 3: Create a new XML file
Create an XML directory in res and then create a paths.xml file in it. A file Provider can only generate a content URI for files in directories that we specify beforehand. We specify a directory, specify its storage area and path in XML, using child elements of the <paths> element. Below is the video of creating the XML file.
XML
<?xml version="1.0" encoding="utf-8"?>
<paths>
<cache-path
name= "shared_images"
path= "images/" />
</paths>
Step 4: Working with the activity_main.xml file
Go to the activity_main.xml file and refer to the following code. Below is the code for the activity_main.xml file.
Reference article: How to Add Image to Drawable Folder in Android Studio?
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:gravity= "center"
android:orientation= "vertical"
tools:context= ".MainActivity" >
<!-- Here we will attach our image to share -->
<ImageView
android:id= "@+id/shareimage"
android:layout_width= "200dp"
android:layout_height= "200dp"
android:src= "@drawable/image" />
<!-- We will click on it then shareonlytext
function will be called-->
<Button
android:id= "@+id/share"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_marginTop= "10dp"
android:background= "@color/colorPrimary"
android:padding= "10dp"
android:text= "Click here to Share "
android:textSize= "10dp" />
</LinearLayout>
Step 5: 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
import android.content.Intent ;
import android.graphics.Bitmap ;
import android.graphics.drawable.BitmapDrawable ;
import android.net.Uri ;
import android.os.Bundle ;
import android.view.View ;
import android.widget.Button ;
import android.widget.ImageView ;
import android.widget.Toast ;
import androidx.appcompat.app.AppCompatActivity ;
import androidx.core.content.FileProvider ;
import java.io.File ;
import java.io.FileOutputStream ;
public class MainActivity extends AppCompatActivity {
Button share ;
ImageView imageView ;
@Override
protected void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState );
setContentView ( R . layout . activity_main );
share = findViewById ( R . id . share );
imageView = findViewById ( R . id . shareimage );
// initialising text field where we will enter data
share . setOnClickListener ( new View . OnClickListener () {
@Override
public void onClick ( View v ) {
// Now share image function will be called
// here we will be passing the text to share
// Getting drawable value from image
BitmapDrawable bitmapDrawable = ( BitmapDrawable ) imageView . getDrawable ();
Bitmap bitmap = bitmapDrawable . getBitmap ();
shareImageandText ( bitmap );
}
});
}
private void shareImageandText ( Bitmap bitmap ) {
Uri uri = getmageToShare ( bitmap );
Intent intent = new Intent ( Intent . ACTION_SEND );
// putting uri of image to be shared
intent . putExtra ( Intent . EXTRA_STREAM , uri );
// adding text to share
intent . putExtra ( Intent . EXTRA_TEXT , "Sharing Image" );
// Add subject Here
intent . putExtra ( Intent . EXTRA_SUBJECT , "Subject Here" );
// setting type to image
intent . setType ( "image/png" );
// calling startactivity() to share
startActivity ( Intent . createChooser ( intent , "Share Via" ));
}
// Retrieving the url to share
private Uri getmageToShare ( Bitmap bitmap ) {
File imagefolder = new File ( getCacheDir (), "images" );
Uri uri = null ;
try {
imagefolder . mkdirs ();
File file = new File ( imagefolder , "shared_image.png" );
FileOutputStream outputStream = new FileOutputStream ( file );
bitmap . compress ( Bitmap . CompressFormat . PNG , 90 , outputStream );
outputStream . flush ();
outputStream . close ();
uri = FileProvider . getUriForFile ( this , "com.anni.shareimage.fileprovider" , file );
} catch ( Exception e ) {
Toast . makeText ( this , "" + e . getMessage (), Toast . LENGTH_LONG ). show ();
}
return uri ;
}
}
Output:
Similar Reads
How to Share Text of Your App with Another App and Vice Versa in Android?
Most of the time while using an app we want to share text from the app to another app. While using Many Social Media Platforms we find this feature to be very useful when we want to share information from one app to another. The Android intent resolver is used when sending data to another app as par
5 min read
How to Share a Captured Image to Another Application in Android?
Pre-requisite: How to open a Camera through Intent and capture an image In this article, we will try to send the captured image (from this article) to other apps using Android Studio. Approach: The image captured gets stored on the external storage. Hence we need to request permission to access the
6 min read
How to Share Image From URL with Intent in Android?
In this article, we will see how can we share images and text with Android Intent. In this activity URL of an image to be shared will be given with extra text and we will open the Dialog where the user chooses using which he wants to share this image as shown on the screen. A sample video is given b
5 min read
How to Send Email in Android App Without Using Intent?
It is crucial to establish an email connection in the Android application. This enables us to send important messages to users. For example, when the user joins the app first time sending a welcome email is a common practice. Additionally in some applications if a user signs in from a new location a
5 min read
How to Load Any Image From URL Without Using Any Dependency in Android?
Many applications display images from the internet using third-party APIs like Glide and Picasso to load images. This means that such applications partly depend on these services to keep themselves working fine. To make the application better, one should write their own code rather than depending on
3 min read
How to Load SVG from URL in Android ImageView?
It is seen that many Android apps require to use of high-quality images that will not get blur while zooming. So we have to use high-quality images. But if we are using PNG images then they will get blur after zooming because PNG images are made up of pixels and they will reduce their quality after
4 min read
How to Use Glide Image Loader Library in Android Apps?
Glide, like Picasso, can load and display images from many sources, while also taking care of caching and keeping a low memory impact when doing image manipulations. Official Google apps (like the app for Google I/O 2015) are using Glide. In this article, we're going to explore the functionalities o
4 min read
How to Make a Meme Sharing App in Android Studio?
Pre-requisites: Android App Development Fundamentals for BeginnersGuide to Install and Set up Android StudioAndroid | Starting with first app/android projectAndroid | Running your first Android app In this article, we will learn how to create an app using Kotlin where memes will be fetched from Redd
5 min read
How to Add Share Button in Toolbar in Android?
In this article, we are going to create a simple Share Button in the Toolbar in Android. The Share Button is used to share information on mail, Bluetooth, Facebook, Twitter, WhatsApp, etc to an individual person or a group on any social media. We can share any type of message like text, images, vide
4 min read
How to Select an Image from Gallery in Android?
Selecting an image from a gallery in Android is required when the user has to upload or set their image as a profile picture or the user wants to send a pic to the other. So in this article, it's been discussed step by step how to select an image from the gallery and preview the selected image. Have
4 min read