Home  >  Android

Android AssetManager Example to Load Image from assets Folder

By Arvind Rai, August 25, 2015
This page will cover Android AssetManager example to load image from assets folder. We keep text, images, videos, pdf etc in assets directory. android.content.res.AssetManager is used to access raw asset from /assets folder. Using this API, we can open the asset for the given asset path and can list all assets for the given folder path. We can instantiate AssetManager in our Activity as follows.
AssetManager assetManager = getAssets();
 

AssetManager.open()

It opens an asset for the given path. Suppose we need to open an image kept in /assets/img/image1.png, the path will be used as img/image1.png. Find the code snippet.
InputStream is = assetManager.open("img/image1.png");
Bitmap  bitmap = BitmapFactory.decodeStream(is);
imageView.setImageBitmap(bitmap);  
AssetManager.open() returns InputStream that is used with BitmapFactory.decodeStream() that returns Bitmap which is used with imageView.setImageBitmap().


AssetManager.list()

To list all the assets for the given folder within /assets folder, we use AssetManager.list(). Suppose we have some files within /assets/img and we need to list all those files, then we write code as follows.
String[] imgPath = assetManager.list("img");
 
Here we get String array of file names within img directory.

Complete Example

Find the project structure in eclipse.
Android AssetManager Example to Load Image from assets Folder
Now find the Activity class and XML files used in the example.
MainActivity.java
package com.concretepage;
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.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
public class MainActivity extends Activity {
	private final static String TAG = "MainActivity"; 
	private ImageView imageView;
	private ImageView imageViewbyCode;
	private LinearLayout myLayout;
	private AssetManager assetManager;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		imageView = (ImageView)findViewById(R.id.image);
		myLayout = (LinearLayout)findViewById(R.id.myLayout);
   	        assetManager = getAssets();
	}
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
               getMenuInflater().inflate(R.menu.menu_main, menu);
               return true;
        }
        public void displayOneImage(View v) {
  	    if (imageViewbyCode != null) {
  		    imageViewbyCode.setVisibility(View.GONE);
  	    }
  	    imageView.setVisibility(View.VISIBLE);
            try {
			InputStream is = assetManager.open("img/image1.png");
			Bitmap  bitmap = BitmapFactory.decodeStream(is);
			imageView.setImageBitmap(bitmap);
	    } catch (IOException e) {
			Log.e(TAG, e.getMessage());
	    }
        }
        public void listAllImages(View v) {
  	    if (imageView != null) {
  	    	imageView.setVisibility(View.GONE);
  	    }
    	    try {
			String[] imgPath = assetManager.list("img");
			for (int i = 0; i< imgPath.length; i++) {
				InputStream is = assetManager.open("img/"+imgPath[i]);
				Log.d(TAG, imgPath[i]);
				Bitmap  bitmap = BitmapFactory.decodeStream(is);
				
				imageViewbyCode = new ImageView(this);
				imageViewbyCode.setImageBitmap(bitmap);
        	                LinearLayout.LayoutParams params =  new LinearLayout
    			            .LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        	                imageViewbyCode.setLayoutParams(params);        
    	                        myLayout.addView(imageViewbyCode);
			}
	     } catch (IOException e) {
			Log.e(TAG, e.getMessage());
	     }
    }
} 

res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"    
    tools:context=".MainActivity"
    android:id="@+id/myLayout">
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btnmsg1"
        android:onClick="displayOneImage"/>
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btnmsg2"
        android:onClick="listAllImages"/>
    <ImageView
	android:id="@+id/image"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:background="#000000"
	android:gravity="center"
	android:padding="32dp"
	android:contentDescription="@string/imgDesc"/>  
</LinearLayout> 	 

Output

1. On click of Display Single Image.
Android AssetManager Example to Load Image from assets Folder
2. On click of List All Images
Android AssetManager Example to Load Image from assets Folder
Output in LogCat.
08-25 12:58:19.680: D/MainActivity(757): image1.png
08-25 12:58:19.710: D/MainActivity(757): image2.png
08-25 12:58:19.790: D/MainActivity(757): image3.png 

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us