![]() |
VOOZH | about |
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 files from the user. So take permission to access external storage permission in the manifest file.
Here pictureDir(File) is pointing to the directory of external storage named DIRECTORY_PICTURES
File pictureDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"CameraDemo");
In the onCreate() method, check whether the directory pictureDir is present or not. If not then create the directory with the code below
if(!pictureDir.exists()) {
pictureDir.mkdirs();
}
Create another method called callCameraApp() to get the clicked image from external storage.
Share this image to other apps using intent.
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
For the sake of this article, we will be selecting Gmail and will be sending this image as an attachment in a mail.
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. The code for that has been given in both Java and Kotlin Programming Language for Android.
Navigate to the app > AndroidManifest.xml file and add the below permissions to it.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Next, go to the activity_main.xml file, which represents the UI of the project. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.
Go to the MainActivity File and refer to the following code. Below is the code for the MainActivity File. Comments are added inside the code to understand the code in more detail.