Android 按路径显示图像

发布于 2024-09-18 16:24:19 字数 569 浏览 4 评论 0原文

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

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

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

但这不起作用我想使用其路径而不是名称来访问图像

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温柔戏命师 2024-09-25 16:24:20

使用 openRawResource() 读取字节流,

这样的事情应该可以工作

InputStream is = context.getResources().openRawResource(R.raw.urfilename);

检查此链接

http://developer.android.com/guide/topics/resources/accessing-resources.html#ResourcesFromCode

它清楚地说明了以下内容

虽然不常见,但您可能需要访问原始文件和目录。如果您这样做,那么将文件保存在 res/ 中将不起作用,因为从 res/ 读取资源的唯一方法是使用资源 ID

如果您想提供一个像上面提到的那样的文件名在您的代码中,您可能需要将其保存在资产文件夹中。

read a stream of bytes using openRawResource()

some thing like this should work

InputStream is = context.getResources().openRawResource(R.raw.urfilename);

Check this link

http://developer.android.com/guide/topics/resources/accessing-resources.html#ResourcesFromCode

It clearly says the following

While uncommon, you might need access your original files and directories. If you do, then saving your files in res/ won't work for you, because the only way to read a resource from res/ is with the resource ID

If you want to give a file name like the one mentioned in ur code probably you need to save it on assets folder.

和我恋爱吧 2024-09-25 16:24:20

您也许可以将 Resources.getIdentifier(name, type, package) 与原始文件一起使用。这将为您获取 id,然后您可以继续使用 setImageResource(id) 或其他任何操作。

int id = getResources().getIdentifier("3150-MCM", "raw", getPackageName());
if (id != 0) //if it's zero then its not valid
   image.setImageResource(id);

是你想要的吗?虽然它可能不喜欢多个文件夹,但值得一试。

You might be able to use Resources.getIdentifier(name, type, package) with raw files. This'll get the id for you and then you can just continue with setImageResource(id) or whatever.

int id = getResources().getIdentifier("3150-MCM", "raw", getPackageName());
if (id != 0) //if it's zero then its not valid
   image.setImageResource(id);

is what you want? It might not like the multiple folders though, but worth a try.

生生不灭 2024-09-25 16:24:20

尝试 {
// 获取 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();
  } 

这里的图像目录是资产的路径,

例如

asset ->图像 ->某个文件夹 -> some.jpg

那么路径将是

image/somefolder/some.jpg

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

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

like

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

then path will be

image/somefolder/some.jpg

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文