Introduction
In this article, we will learn how to make your Image viewer handle Image files with intent filters in Android.
Here, we create an app that can open an Image file. When you click on an image file, it displays a list of applications that can handle it; therefore, after you install your app, it will appear in the list if a user clicks on your app's logo and views an image file displayed in your app. This functionality is supported due to the VIEW intent filter.
We will also include a SEND intent filter in this app, allowing it to appear on a list whenever an Image file is shared. Therefore, when someone taps on the logo of your app, an image file opens in it.
Create an Image Viewer App that handles VIEW And SEND Intent Filter
Step 1
Create a new project in the Android Studio and select an empty activity.
Step 2
Name the project, select the save location folder, and click the finish button.
Step 3
Create the activity_main.xml file as shown below.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageViewer"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Step 4
Declare ImageView objects and declare the variable EXT_STORAGE_PERMISSION_CODE to take permission from the user for READ_EXTERNAL_STORAGE in the MainActivity.java class.
private final int EXT_STORAGE_PERMISSION_CODE = 1;
private ImageView imageView;
Step 5
Now take the READ_EXTERNAL_STORAGE permission from the user in the onCreate() method in MainActivity.java class.
if (ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[] {
android.Manifest.permission.READ_EXTERNAL_STORAGE
}, EXT_STORAGE_PERMISSION_CODE);
Log.d("PERMISSION", "After getting permission: " + android.Manifest.permission.READ_EXTERNAL_STORAGE + " " + ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE));
} else {
Log.d("PERMISSION", "Already has permission to read to external storage");
}
Step 6
Include the READ_EXTERNAL_STORAGE permission in the manifest file.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Step 7
Get the intent and know whether it is a send or a view intent before accessing the URI. And set the URI to our imageView object in the MainActivity.java class's onCreate() method.
Intent intent = getIntent();
Uri uri;
imageView = findViewById(R.id.imageViewer);
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
uri = intent.getData();
imageView.setImageURI(uri);
}
if (Intent.ACTION_SEND.equals(intent.getAction())) {
uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
imageView.setImageURI(uri);
}
Final MainActivity.Java File
package com.example.imageviewer;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private final int EXT_STORAGE_PERMISSION_CODE = 1;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ContextCompat.checkSelfPermission(
MainActivity.this,
android.Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions(
MainActivity.this,
new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE},
EXT_STORAGE_PERMISSION_CODE);
Log.d("PERMISSION", "After getting permission: "+ android.Manifest.permission.READ_EXTERNAL_STORAGE + " " + ContextCompat.checkSelfPermission(
MainActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE));
} else {
Log.d("PERMISSION", "Already has permission to read to external storage");
}
Intent intent = getIntent();
Uri uri;
imageView = findViewById(R.id.imageViewer);
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
uri = intent.getData();
imageView.setImageURI(uri);
}
if (Intent.ACTION_SEND.equals(intent.getAction())) {
uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
imageView.setImageURI(uri);
}
}
}
Step 8
In the manifest file in your activity, define the intent view and send as follows.
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.OPENABLE" />
<data android:scheme="content" />
<data android:mimeType="image/*" />
<data android:host="*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
</activity>
The first intent in the list above is for Launcher activity, the second is for View Or Open, and the third is for Send Or Share.
Step 9
Run your app now, and when it asks for permission, provide it. After that, exit the app and try to open any Image file from the file manager. You will see a list of different apps that can open Image files; your app should now appear on that list and in the share options.
Summary
So you see, creating an Image Viewer In Android with little coding is very easy.
In this article, we learned how to make your image viewer handle image files with intent filters in Android.
If you like my article, please like this article and comment with your views on it.
Thank you. Enjoy Coding.