从资产Android开位图或声音 [英] android get Bitmap or sound from assets

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

问题描述

我需要得到位图,并从资产声音。我试着这样做:

  BitmapFactory.de codeFILE(文件:///android_asset/Files/Numbers/l1.png);
 

和这样的:

  getBitmapFromAsset(文件/号/ l1.png);
    私人位图getBitmapFromAsset(字符串则strName){
        AssetManager assetManager = getAssets();
        InputStream的ISTR = NULL;
        尝试 {
            ISTR = assetManager.open(则strName);
        }赶上(IOException异常E){
            e.printStackTrace();
        }
        点阵位图= BitmapFactory.de codeStream(ISTR);
        返回的位图;
    }
 

不过,我得到的只是自由空间,没有图像。

如何做到这一点?

解决方案

 公共静态位图getBitmapFromAsset(上下文的背景下,字符串文件路径){
    AssetManager assetManager = context.getAssets();

    InputStream的ISTR;
    点阵位图= NULL;
    尝试 {
        ISTR = assetManager.open(文件路径);
        位= BitmapFactory.de codeStream(ISTR);
    }赶上(IOException异常E){
        //处理异常
    }

    返回的位图;
}
 

的路径就是你的文件名FX bitmap.png。如果你使用的子文件夹中的位图/那么它的位图/ bitmap.png

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

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

And like this:

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.

How to do this?

解决方案

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

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

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

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