Showing Image View From File Path in Android
Last Updated :
17 Jul, 2022
Image Views are used to display images in different formats within the Android Application. We can display images within our Image View from the image file name, bitmap, drawable file, and the image URL as well. In this article, we will take a look at How to load images from user devices within Image View using the image path within our Android Application.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Project just refer to this article on How to Create New Project in Android Studio. The code has been given in both Java and Kotlin Programming Language for Android.
Step 2: Working with activity_main.xml
Navigate to app > res > layout > activity_main.xml and add the below code to it. Comments are added in the code to get to know in detail.
XML
<? xml version = "1.0" encoding = "utf-8" ?>
android:id = "@+id/container"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:orientation = "vertical"
tools:context = ".MainActivity" >
< TextView
android:id = "@+id/idTVHeading"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_above = "@id/idIVImage"
android:layout_margin = "20dp"
android:gravity = "center"
android:padding = "4dp"
android:text = "Showing Image View from File Path"
android:textAlignment = "center"
android:textColor = "@color/purple_200"
android:textSize = "20sp"
android:textStyle = "bold" />
< ImageView
android:id = "@+id/idIVImage"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_centerInParent = "true" />
</ RelativeLayout >
|
Step 3: Adding Permissions in our AndroidManifest.xml
As we are displaying images from users’ devices within our image view. We have to provide permission to read external storage. Navigate to app>AndroidManifest.xml and add the below permissions to it.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Step 4: Working with MainActivity File
Navigate to app > java > your app’s package name > MainActivity File (Java or Kotlin) and add the below code to it. Comments are added in the code to get to know in detail.
Kotlin
import android.graphics.BitmapFactory
import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import java.io.File
class MainActivity : AppCompatActivity() {
lateinit var imageIV: ImageView
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val imgFile = File( "/storage/emulated/0/Download/GeekforGeeksphoto.png" )
imageIV = findViewById(R.id.idIVImage)
if (imgFile.exists()) {
val imgBitmap = BitmapFactory.decodeFile(imgFile.absolutePath)
imageIV.setImageBitmap(imgBitmap)
}
}
}
|
Java
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import java.io.File;
public class MainActivity extends AppCompatActivity {
ImageView imageIV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File imgFile = new File( "/storage/emulated/0/Download/GeekforGeeksphoto.png" );
imageIV = (ImageView) findViewById(R.id.idIVImage);
if (imgFile.exists()) {
Bitmap imgBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
imageIV.setImageBitmap(imgBitmap);
}
}
}
|
Now run your application to see the output of it.
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...