Show/Load image inside ImageView from assets folder in android

How to dynamically retrieve assets folder images and set into ImageView.

In this tutorial we are simply putting image inside assets folder which is used to hold any type of files inside android application package. So we are using bitmap’s method with AssetManager including input stream to call image file directly from Assets folder and set into ImageView . So here is the complete step by step tutorial for Show/Load image inside ImageView from assets folder in android

android-project-download-code-button

Note : Please upload sample image inside assets folder.

assets folder android

Copy the below image into Assets folder.

sample_drawable_image

How to Show/Load image inside ImageView from assets folder in android.

Code for MainActivity.java file.

 package com.loadimageinsideimageviewfromassets_android_examples.com;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

 ImageView imageview;
 Button button;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 imageview = (ImageView)findViewById(R.id.imageView1);
 button = (Button)findViewById(R.id.button1);
 
 button.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 //Pass your image name which you have copy inside assets folder here with image extension.
 imageview.setImageBitmap(ImageViaAssets("sample_drawable_image.png"));
 
 
 }
 });
 }
 
 public Bitmap ImageViaAssets(String fileName){
 
 AssetManager assetmanager = getAssets();
 InputStream is = null;
 try{
 
 is = assetmanager.open(fileName);
 }catch(IOException e){
 e.printStackTrace();
 }
 Bitmap bitmap = BitmapFactory.decodeStream(is);
 return bitmap;
 }
 
}

Code for activity_main.xml layout file.

 <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.loadimageinsideimageviewfromassets_android_examples.com.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentBottom="true"
 android:layout_centerHorizontal="true"
 android:layout_marginBottom="146dp"
 android:text="Click here to Show image inside ImageView from assets folder in android" />

 <ImageView
 android:id="@+id/imageView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_above="@+id/button1"
 android:layout_centerHorizontal="true"
 />

</RelativeLayout>

Screenshot:

Show/Load image inside ImageView from assets folder in android

Click here to download Show/Load image inside ImageView from assets folder in android project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *