我想编写一个将位图另存为文件并进行检索的测试。我创建了一个宽度和高度为200的位图,并将其写在文件上。但是,当我从文件中读取文件时,由于某些错误,其宽度始终返回100。
@RunWith(RobolectricTestRunner::class)
class ViewModelTest {
private var tempFile: File? = null
@Before
@Throws(Exception::class)
fun setUp() {
val cacheDir = ApplicationProvider.getApplicationContext<Application>().cacheDir
cacheDir.mkdir()
tempFile = File(cacheDir, "temp.jpeg")
}
@Test
fun bitmapTest() {
val sampleBitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888)
if (tempFile?.exists() == true)
tempFile?.delete()
try {
val out = FileOutputStream(tempFile)
sampleBitmap?.compress(Bitmap.CompressFormat.JPEG, 100, out)
out.flush()
out.close()
} catch (e: Exception) {
e.printStackTrace()
}
val bitmap = BitmapFactory.decodeStream(FileInputStream(tempFile))
assertEquals(200, bitmap.height)
}
}