如何加载从资产的形象? [英] How to load a image from assets?

查看:151
本文介绍了如何加载从资产的形象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要加载从资产的图像,以避免升级Froyo 2.2.2错误调整POT图像在某些特殊情况下。的方式,以避免它是从资产目录加载的图像文件。

i need to load a image from assets to avoid a froyo 2.2.2 bug resizing POT images in some particular cases. The way to avoid it is loading the image files from assets dir.

我想用这样的:

        String imagePath = "radiocd5.png";
    AssetManager mngr = context.getAssets();
    // Create an input stream to read from the asset folder
    InputStream is=null;
    try {
        is = mngr.open(imagePath);
    } catch (IOException e1) {  e1.printStackTrace();}

    //Get the texture from the Android resource directory
    //InputStream is = context.getResources().openRawResource(R.drawable.radiocd5);
    Bitmap bitmap = null;
    try {
        //BitmapFactory is an Android graphics utility for images
        bitmap = BitmapFactory.decodeStream(is);

    } finally {
        //Always clear and close
        try {
            is.close();
            is = null;
        } catch (IOException e) {
        }
    }

但我得到的NullPointerException就行了 is.close();

我捕获FileNotFoundException异常:radiocd5.png,但该文件是对我的资产目录:•

i capture a FileNotFoundException: radiocd5.png, but that file is on my assets directory :S

我在做什么不好?该文件称为 radiocd5.png ,它是在资产目录

What am i doing bad? The file is called radiocd5.png and it is on the assets directory

推荐答案

您可以按照我的教程从资产中显示的数据:的 HTTPS://xjaphx.word$p$pss.com/2011/10/02/store-and-use-files-在-资产/
与装载图片和文字的样品显示。

You can follow my tutorials on displaying data from Assets: https://xjaphx.wordpress.com/2011/10/02/store-and-use-files-in-assets/
The sample with loading image and text to display.

我现在又增加所提供的链接的相关部分 (如遇地震或东西);-) TAIFUN

I now added the relevant part of the provided link (in case of earthquake or something) ;-) Taifun

// load image
try {
    // get input stream
    InputStream ims = getAssets().open("avatar.jpg");
    // load image as Drawable
    Drawable d = Drawable.createFromStream(ims, null);
    // set image to ImageView
    mImage.setImageDrawable(d);
}
catch(IOException ex) {
    return;
}

这篇关于如何加载从资产的形象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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