如何从资产加载图像? [英] How to load a image from assets?

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

问题描述

我需要从资产加载图像以避免在某些特定情况下调整 POT 图像大小的 froyo 2.2.2 错误.避免它的方法是从资产目录加载图像文件.

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) {}
}

但是我在行 is.close();

我捕获了一个 FileNotFoundException:radiocd5.png,但该文件在我的资产目录中:S

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

我做错了什么?该文件名为 radiocd5.png,位于 assets 目录

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

推荐答案

您可以按照我的教程来显示来自 Assets 的数据:https://xjaphx.wordpress.com/2011/10/02/store-and-use-files-in-assets/
加载图像和文本以显示的示例.

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天全站免登陆