How to load an image from the assets folder in android?

By | August 9, 2012

Here is a simple example showing how to load an image stores in assets folder in android?

package com.coderzheaven.pack;
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.widget.ImageView;

public class LoadImageFromAssetsDemo extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // Get the AssetManager
        AssetManager manager = getAssets();

        // Read a Bitmap from Assets
        try {
        	InputStream open = manager.open("icon.png");
        	Bitmap bitmap = BitmapFactory.decodeStream(open);
        	// Assign the bitmap to an ImageView in this layout
        	ImageView view = (ImageView) findViewById(R.id.ImageView01);
        	view.setImageBitmap(bitmap);
        } catch (IOException e) {
        	e.printStackTrace();
        } 
    }
}

Done.

Leave a Reply

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