我试着把图像保存到我手机的内部存储器里。在尝试了几种保存文件的方法之后,我设法保存了这张照片,但它本身并没有打开。我可以在手机的内部存储中找到这张照片并打开它(android将该文件识别为照片,因为它在图库中自动打开),但照片没有显示。
当打开照片时,我看到了以下内容:照片在电话中,这张照片需要0 In。
我的功能是将照片保存到内部存储:
fun saveImageToStorage(urlImage: String, pathTitle: String) : String{
val dirs = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)
val dir = File(dirs?.absolutePath, pathTitle)
if(!dir.exists()){
dir.mkdir()
}
val file = File(dir, System.currentTimeMillis().toString() + ".jpg")
val bitmap: Bitmap = downloadImage(urlImage)
try {
file.createNewFile()
val imageStream = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, imageStream)
val image = imageStream.toByteArray()
Log.i("IMAGE SIZE", "${image.size}")
imageStream.write(image)
imageStream.flush()
imageStream.close()
}catch (e: Exception){
e.message
}
Log.i("INFOOOOOO", file.absolutePath)
return file.absolutePath
}上传图片并获取位图。
fun downloadImage(urlImage: String): Bitmap {
return Glide
.with(context)
.asBitmap()
.timeout(60_000)
.load(urlImage)
.fitCenter()
.submit()
.get()
}我已经连接了依赖项
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>我试着做的仪器测试。它位于src/AndroidTest中。
@Test
fun saveAdnGetPhoto(){
val path = daoAdapter.saveImageToExternalStorage("https://www.gstatic.com/youtube/src/web/htdocs/img/tv_stack.png", "Naruto")
Awaitility.await().atMost(2, TimeUnit.SECONDS).untilAsserted {
val output = daoAdapter.getPhotoFromStorage(path)
assertEquals(true, output != null)
}
}试图沿保存的路径获取照片时出错
java.io.FileNotFoundException: /storage/emulated/0/Android/data/com.example.mangaua/files/Pictures/Naruto/1661511946507.jpg
at android.content.res.AssetManager.openAsset(Native Method)
at android.content.res.AssetManager.open(AssetManager.java:690)
at android.content.res.AssetManager.open(AssetManager.java:664)
at com.example.mangaua.menu.connector.DownloadPageChapter.getPhoto(DownloadPageChapter.kt:95)
at com.example.mangaua.menu.connector.ConnectorBaseAndParse.getPhotoFromStorage(ConnectorBaseAndParse.kt:443)
at com.example.mangaua.TestDownloadAndSaveImageToExternal.saveAdnGetPhoto$lambda-0(TestDownloadAndSaveImageToExternal.kt:72)
at com.example.mangaua.TestDownloadAndSaveImageToExternal.$r8$lambda$jm-yYcWapmPfDqI53v3Npy212R8(Unknown Source:0)
at com.example.mangaua.TestDownloadAndSaveImageToExternal$$ExternalSyntheticLambda0.run(Unknown Source:4)
at org.awaitility.core.AssertionCondition$1.eval(AssertionCondition.java:55)
at org.awaitility.core.ConditionAwaiter$ConditionPoller.call(ConditionAwaiter.java:201)
at org.awaitility.core.ConditionAwaiter$ConditionPoller.call(ConditionAwaiter.java:188)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:784)返回照片的功能
fun getPhoto(pathImage: String) : Bitmap{
return BitmapFactory.decodeStream(InstrumentationRegistry.getInstrumentation().context.assets.open(pathImage))
}调试模式
谢谢你的回答,我通常不喜欢在堆叠溢出上创建主题,因为我总是能找到答案,但这一次我会在周日左右践踏一个地方。
发布于 2022-08-26 17:48:37
我认为您可以使用FileOutputStream而不是ByteArrayOutputStream。
https://stackoverflow.com/questions/73500651
复制相似问题