我在哪里放置图像资源文件? BitmapFactory.decodeFile() [英] Where do I put the image resource file? BitmapFactory.decodeFile()
问题描述
我试图使用这个:
BitmapFactory.decodeFile(logo.jpg)
BitmapFactory.decodeFile("logo.jpg")
我似乎没有将文件:logo.jpg放在正确的地方,与我的apk相比。应该去哪里?
and I don't seem to have the file: logo.jpg in the right place in comparison to my apk. where should it go?
我得到的错误是:
编辑
我现在使用的是:> BitmapFactory.decodeResource(getActivity()。getResources(),logo.jpg)
I now am using this: >BitmapFactory.decodeResource(getActivity().getResources(),"logo.jpg")
并且现在得到一个编译器错误,说:
and am now getting a compiler error that says:
方法getActivity )是未定义的类型砖
(砖是类的名称)
The method getActivity() is undefined for the type Brick (Brick is the name of the class)
我不在乎哪个解决方案只要其中一个工作,就可以工作
I don't care which solution works as long as one of them does
推荐答案
我想你想要做的是将JPG文件放入您的项目并通过Java代码加载,是正确的。
I think what you are trying to do is to put an JPG file into your project and load via Java code, is that correct.
如果是这样,你需要把你的 logo.jpg
到您的资产
文件夹,
If so, you'll need to put your logo.jpg
into your assets
folder,
并使用与此SO答案所述相似的方法加载:
and load it using the similar method as this SO answer stated:
https:// stackover flow.com/a/8502231/763459
为方便起见,我粘贴了以下代码: / p>
For your convenience I pasted the code as below:
InputStream bitmap=null;
try {
bitmap=getAssets().open("logo.png");
Bitmap bit=BitmapFactory.decodeStream(bitmap);
img.setImageBitmap(bit);
} catch (IOException e) {
e.printStackTrace();
} finally {
if(bitmap!=null)
bitmap.close();
}
这篇关于我在哪里放置图像资源文件? BitmapFactory.decodeFile()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!