Saturday, November 16, 2024
Google search engine
HomeLanguagesJavaShowing Image View From File Path in Android

Showing Image View From File Path in Android

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"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
  
    <!-- on below line we are creating a text for heading of our app -->
    
    <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" />
  
    <!-- on below line we are creating an image view for displaying a bitmap in it -->
    
    <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() {
  
    // on below line we are creating variable for image view.
    lateinit var imageIV: ImageView
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // on below line we are creating an image file and
        // specifying path for the image file on below line.
        val imgFile = File("/storage/emulated/0/Download/GeekforGeeksphoto.png")
  
        // on below line we are initializing variables with ids.
        imageIV = findViewById(R.id.idIVImage)
  
        // on below line we are checking if the image file exist or not.
        if (imgFile.exists()) {
  
            // on below line we are creating an image bitmap variable 
            // and adding a bitmap to it from image file.
            val imgBitmap = BitmapFactory.decodeFile(imgFile.absolutePath)
  
            // on below line we are setting bitmap to our image view.
            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 {
  
    // on below line we are creating variable for image view.
    ImageView imageIV;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // on below line we are creating an image file and 
        // specifying path for the image file on below line.
        File imgFile = new File("/storage/emulated/0/Download/GeekforGeeksphoto.png");
  
        // on below line we are initializing variables with ids.
        imageIV = (ImageView) findViewById(R.id.idIVImage);
  
        // on below line we are checking if the image file exist or not.
        if (imgFile.exists()) {
            // on below line we are creating an image bitmap variable
            // and adding a bitmap to it from image file.
            Bitmap imgBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
  
            // on below line we are setting bitmap to our image view.
            imageIV.setImageBitmap(imgBitmap);
        }
    }
}


 Now run your application to see the output of it. 

Output:

Show Image View from File Path

 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments