Is it possible to load a drawable from the assets folder? Is it possible to load a drawable from the assets folder? android android

Is it possible to load a drawable from the assets folder?


Hope this help:

Drawable d = Drawable.createFromStream(getAssets().open("Cloths/btn_no.png"), null);


I recommend to use this

 Drawable.createFromResourceStream(resources,new TypedValue(), resources.getAssets().open(filename), null)

which returns properly scaled drawable thanks to resources ...


Here's a class with static method to get the drawable from the assets. It also closes the inputstream.

import android.content.Context;import android.graphics.drawable.Drawable;import java.io.IOException;import java.io.InputStream;/** * Created by bartburg on 4-11-2015. */public class AssetsReader {    public static Drawable getDrawableFromAssets(Context context, String url){        Drawable drawable = null;        InputStream inputStream = null;        try {            inputStream = context.getAssets().open(url);            drawable = Drawable.createFromStream(inputStream, null);        } catch (IOException e) {            e.printStackTrace();        } finally {            if(inputStream != null) {                try {                    inputStream.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }        return drawable;    }}