无法从drawable(Robolectric)获取位图

时间:2013-08-16 09:40:45

标签: android unit-testing android-drawable robolectric

我正在尝试检索bitmap并保存到PC上的本地磁盘。 结果我得到了一个无法读取的jpeg文件(95字节)。

源:

Application application = new MyApplication();
Bitmap bitmap = BitmapFactory.decodeResource(application.getResources(), drawableId);

File outputFile = new File("D:\\OutputFile.jpg");
OutputStream os = new FileOutputStream(outputFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
IoUtils.closeSilently(os); //close stream

P.S。我知道我无法从bitmap获得drawable。但我不知道为什么......

1 个答案:

答案 0 :(得分:0)

因此,请尝试将Bitmap类型图像转换为Drawable

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