将Bitmap转换为Drawable不显示任何内容

时间:2012-02-04 06:45:47

标签: android bitmap drawable

我正在使用以下代码将Bitmap转换为Drawable。

MemoryCache memoryCache = new MemoryCache();
Bitmap bitmap = memoryCache.get(thumbnail);
Drawable drawable = (Drawable)new BitmapDrawable(bitmap); 

我使用的MemoryCache来自LazyList Project,当我使用位图时它工作正常,但当我将其转换为drawable时,它没有代替图像显示任何内容。

请帮助

3 个答案:

答案 0 :(得分:18)

更改此行:

Drawable drawable = (Drawable)new BitmapDrawable(bitmap);

要:

Drawable drawable = new BitmapDrawable(getResources(), bitmap);

请注意,BitmapDrawable(Bitmap bitmap)构造函数已被弃用(source),并且使用上述调用显然可确保使用正确的目标密度正确设置它。

答案 1 :(得分:3)

尝试使用以下

Drawable d = new BitmapDrawable(bitmap);

答案 2 :(得分:0)

发现问题

实际上问题在于

MemoryCache memoryCache = new MemoryCache();
Bitmap bitmap = memoryCache.get(thumbnail);
相关问题