Android按路径显示图像 [英] Android Show image by path

查看:100
本文介绍了Android按路径显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在imageview中显示图像而不使用id.

i want to show image in imageview without using id.

我会将所有图像放在原始文件夹中,然后打开

i will place all images in raw folder and open

     try {
            String ss = "res/raw/images/inrax/3150-MCM.jpg";
             in = new FileInputStream(ss);
        buf = new BufferedInputStream(in);
        Bitmap bMap = BitmapFactory.decodeStream(buf);
        image.setImageBitmap(bMap);
        if (in != null) {
         in.close();
        }
        if (buf != null) {
         buf.close();
        }
    } catch (Exception e) {
        Log.e("Error reading file", e.toString());
    }

但这无法正常工作,我想使用其路径而不是按名称访问图像

but this is not working i want to access image using its path not by name

推荐答案

尝试{ //获取对AssetManager的引用
AssetManager mngr = getAssets();

try { // Get reference to AssetManager
AssetManager mngr = getAssets();

        // Create an input stream to read from the asset folder
        InputStream ins = mngr.open(imdir);

        // Convert the input stream into a bitmap
        img = BitmapFactory.decodeStream(ins);

  } catch (final IOException e) {
        e.printStackTrace();
  } 

此处的图片目录是资产的路径

here image directory is path of assets

喜欢

确定->图片-> somefolder-> some.jpg

assest -> image -> somefolder -> some.jpg

然后路径将

image/somefolder/some.jpg

image/somefolder/some.jpg

现在不需要图像的资源ID,您可以使用它在运行时填充图像

now no need of resource id for image , you can populate image on runtime using this

这篇关于Android按路径显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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