Human
Human

Reputation: 10815

BitmapFactory.decodeFile in asset doesn't work (FileNotFindException)

I don't know why decodeFile doesn't work when the param point to a file inside asset folder.

     // Load images from the file path
    String[] dir = null;
    try {
        dir = GenericMainContext.sharedContext.getAssets().list("drawable");
       // dir Log => [ic.png, ic_info_dark.png, ic_launcher_default.png]
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    for (String uri : dir){ 
        // do your stuff here
        if (uri!=null) {
            Bitmap bitmap = null;
            try {
                bitmap = BitmapFactory.decodeFile("file:///android_asset/drawable/"+uri);               } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

Any explanation ?

Upvotes: 3

Views: 2823

Answers (1)

Dahlgren
Dahlgren

Reputation: 781

file:///android_asset/ is used by WebView to load assets. To load assets programmatically use the getAssets() method which returns an AssetMAnager on a Context object such as an Activity. The open(filename) will return a InputStream to your asset file. The following will create a Bitmap from your asset file fileName inside an Activity,

BitmapFactory.decodeStream(getAssets().open(fileName))

Upvotes: 5

Related Questions