[英]Android : cannot read image in assets folder and assign to imageview
I have a file food.jpg
in assets folder. 我在资产文件夹中有一个文件
food.jpg
。 I have try many ways. 我尝试了很多方法。 but I still cannot get this image into imageView.
但我仍然无法将此图像导入imageView。 when run, instead of display image "food.jpg", it displays image that I have declared in xml file.
运行时,它将显示我在xml文件中声明的图像,而不是显示图像“ food.jpg”。 (that image is in drawable folder).
(该图像在可绘制的文件夹中)。
The first way is: 第一种方法是:
int asset_id = context.getResources().getIdentifier("food", "drawable", context.getPackageName());
imageView.setImageResource(asset_id);
The second way is: 第二种方法是:
AssetManager assetManager = context.getAssets();
InputStream istr;
try {
istr = assetManager.open("food");
Bitmap bitmap = BitmapFactory.decodeStream(istr);
imageView.setImageBitmap(bitmap);
istr.close();
} catch (IOException e) {
e.printStackTrace();
}
Please teach me how to fix this problem. 请教我如何解决此问题。
thanks :) 谢谢 :)
Your code is working fine. 您的代码运行正常。 just add image MIME-Type
(.jpg,jpeg,.png... etc)
in below line: 只需在下面的行中添加图像MIME类型
(.jpg,jpeg,.png... etc)
:
istr = assetManager.open("ceo.jpg");
full code for your refrance 您的refrance的完整代码
AssetManager assetManager = getAssets();
InputStream istr;
try {
istr = assetManager.open("ceo.jpg");
Bitmap bitmap = BitmapFactory.decodeStream(istr);
imageView.setImageBitmap(bitmap);
istr.close();
} catch (IOException e) {
e.printStackTrace();
}
Try this Code... 尝试此代码...
try {
// get input stream
InputStream ims = getAssets().open("food.jpg");
// load image as Drawable
Drawable d = Drawable.createFromStream(ims, null);
// set image to ImageView
imgNews.setImageDrawable(d);
} catch (Exception ex) {
return;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.