java – Android项目中getResourceAsStream的路径

前端之家收集整理的这篇文章主要介绍了java – Android项目中getResourceAsStream的路径前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我试图以编程方式加载.png文件,但我不确定我应该给出的确切路径是什么.我尝试了不同的路径,我认为会让我到资源,但都返回null:

InputStream is = getClass().getClassLoader().getResourceAsStream("playstore.png");
Bitmap bmp = BitmapFactory.decodeStream(is);
mStoreImage.setImageBitmap(bmp);

如果我理解正确,我不能使用资源,因为我正在构建.jar.这是我在Android Studio显示的项目包结构:

enter image description here

最佳答案
你应该使用Resources.openRawResource()

例如如果你的活动

getResources().openRawResource(R.raw.playstore.png);

记得把文件放在res / raw下

编辑:

另外,android有自己的方法获取可绘制的(例如png文件)到Bitmap中

原文链接:https://www.f2er.com/android/431321.html

猜你在找的Android相关文章