简体   繁体   English

无法将图像从原始图像加载到imageview

[英]Can't load image from raw to imageview

I want to load image using setImageResource, from raw folder to imageview, But I get Expected resource of type drawable error. 我想使用setImageResource从原始文件夹加载图像到imageview,但是我得到了drawable错误类型的Expected资源。

I'm using android studio, Before that when I was using Eclipse everything was fine. 我正在使用android studio,在此之前,当我使用Eclipse时,一切都很好。 I don't want to use drawable folder. 我不想使用可绘制文件夹。

错误

raw folder: 原始文件夹:

我项目中的原始文件夹

Any suggestion? 有什么建议吗? Thanks 谢谢

I want to load image using setImageResource, from raw folder to imageview, But I get Expected resource of type drawable error. 我想使用setImageResource从原始文件夹加载图像到imageview,但是我得到了drawable错误类型的Expected资源。

Using a raw resource for setImageResource() is not documented behavior. 没有为setImageResource()使用原始资源没有记录的行为。

Before that when I was using Eclipse everything was fine 在那之前,当我使用Eclipse时,一切都很好

Not really. 并不是的。 While your code runs, it might not on all past, present, and future versions of Android. 在您的代码运行时,它可能并不适用于过去,现在和将来的所有版本。 This is why there is a compiler warning. 这就是为什么有编译器警告的原因。

because drawable scaled down images depend of device 因为可绘制的缩小图像取决于设备

Use drawable-nodpi for drawables that should not be scaled based upon screen density. drawable-nodpi用于不应根据屏幕密度缩放的可绘制对象。 That is usually not a good idea, but perhaps you have a reason for it. 通常这不是一个好主意,但也许您有理由。

A safer approach for using a raw resource would be to use openRawResource() on Resources to get an InputStream , then use BitmapFactory to read in the bitmap. 使用原始资源的一种更安全的方法是在Resources上使用openRawResource()获得InputStream ,然后使用BitmapFactory读取位图。

You are welcome to suppress the warning with @SuppressWarnings("ResourceType") , but do not complain when your app stops working. 欢迎您使用@SuppressWarnings("ResourceType")禁止显示警告,但在您的应用停止运行时不要抱怨。

You can try it 你可以试试

   InputStream imageStream = this.getResources().openRawResource(R.raw.name);
   Bitmap bitmap = BitmapFactory.decodeStream(imageStream);
   imageview.setImageBitmap(bitmap);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM