android从资产中获取位图或声音 [英] android get Bitmap or sound from assets

查看:30
本文介绍了android从资产中获取位图或声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从资产中获取位图和声音.我试着这样做:

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.

如何做到这一点?

推荐答案

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;
}

路径就是您的文件名 fx bitmap.png.如果你使用子文件夹 bitmap/那么它的 bitmap/bitmap.png

the path is simply your file name fx bitmap.png. if you use subfolder bitmap/ then its bitmap/bitmap.png

这篇关于android从资产中获取位图或声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆