Saturday, November 14, 2020

Android get Bitmap or sound from assets

Android get Bitmap or sound from assets

I need to get Bitmap and sound from assets. I try to do like this:

BitmapFactory.decodeFile("file:///android_asset/Files/Numbers/l1.png");

getBitmapFromAsset("Files/Numbers/l1.png");

    private Bitmap getBitmapFromAsset(String strName) {

        AssetManager assetManager = getAssets();

        InputStream istr = null;

        try {

            istr = assetManager.open(strName);

        } catch (IOException e) {

            e.printStackTrace();

        }

        Bitmap bitmap = BitmapFactory.decodeStream(istr);

        return bitmap;

    }

But I get just free space, not image.

Answers

public static Bitmap getBitmapFromAsset(Context context, String filePath) {

    AssetManager assetManager = context.getAssets();


    InputStream istr;

    Bitmap bitmap = null;

    try {

        istr = assetManager.open(filePath);

        bitmap = BitmapFactory.decodeStream(istr);

    } catch (IOException e) {

        // handle exception

    }


    return bitmap;

}

No comments:

Post a Comment