从res / drawable文件夹加载图像时出现意外行为

时间:2018-04-04 11:14:33

标签: android

我使用以下代码从res / drawable文件夹加载图像。 但是,有时它正确加载,而在其他几次加载空白图片。这可能是什么原因?

bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.p1);

2 个答案:

答案 0 :(得分:0)

试试这种方式 getResources().getDrawable(R.drawable.dog)).getBitmap();
如果需要,请尝试将其强制转换为BitmapDrawable ((BitmapDrawable)getResources().getDrawable(R.drawable.dog)).getBitmap();

答案 1 :(得分:0)

这可能是因为API 22 getResources()。getDrawable()deprecated,所以我们可以使用以下解决方案。

Drawable vectorDrawable = ResourcesCompat.getDrawable(context.getResources(), R.drawable.logo, null);
Bitmap myLogo = ((BitmapDrawable) myDrawable).getBitmap();