Get PNG image stored in assets folder - Android App

Android examples for App:Assets Image

Description

Get PNG image stored in assets folder

Demo Code


//package com.java2s;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;

import android.graphics.drawable.Drawable;

public class Main {
    /**/*from  ww w  .  jav  a  2s.co m*/
     * Get image stored in assets folder
     * @param context
     * @param imageType
     * @param imageName
     * @return
     */
    public static Drawable getImageFromAssets(Context context,
            String imageType, String imageName) {
        Drawable d = null;
        InputStream myInput = null;

        try {
            if (imageName == null) {
                return null;
            }
            String srcName = imageType + imageName + ".png";
            myInput = context.getAssets().open(srcName);
            d = Drawable.createFromStream(myInput, srcName);
            myInput.close();
            myInput = null;
        } catch (IOException ex) {
            myInput = null;
        }

        return d;
    }
}

Related Tutorials