我將 uri 保存為 Room 資料庫中的字串,然后應用程式正在檢索資料并顯示 LazyVerticalGrid 中的所有影像。當我添加影像時,一切正常,所有影像都正確顯示。問題是當我重新啟動應用程式時。所有影像都消失了,當我添加完全相同的影像(具有相同的 uri)時 - 它出現在第一個位置,但也出現在我重新啟動應用程式之前的所有位置。當我檢查日志時,所有 uri 也以格式從資料庫中正確檢索:
content://com.android.providers.media.documents/document/image:9371
這是影像顯示的代碼(使用線圈):
@Composable
fun ProfilePhotoView(photo: String, navController: NavHostController) {
Image(
painter = rememberImagePainter(photo),
contentDescription = null,
contentScale = ContentScale.Crop,
modifier = Modifier
.aspectRatio(1f)
.padding(3.dp)
.clip(RoundedCornerShape(10.dp))
.clickable {},
alignment = Alignment.Center
)
Log.i(TAG,"photo string = ${photo}")
}
這是我在添加到資料庫之前檢索 uri 的方法:
val selectImagelauncher = rememberLauncherForActivityResult(
contract =
ActivityResultContracts.GetContent()
) { uri: Uri? ->
imageUriState = uri
}
selectImagelauncher.launch("image/*")
您知道為什么在將相同的影像添加到資料庫之前我無法顯示帶有“舊”uri 的影像嗎?
uj5u.com熱心網友回復:
我將 uri 保存為 Room 資料庫中的字串
使用您現有的代碼,這是行不通的。通常,您只有從接收Uri. 一旦您的行程終止,所有訪問權限都將丟失。
如果你改變ActivityResultContracts.GetContent()對ActivityResultContracts.OpenDocument()了,你叫takePersistableUriPermission()上ContentResolver了Uri,你得到的,那么你對這些內容長期訪問。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/386353.html
標籤:安卓 android-room 撰写
上一篇:為改變布局高度添加影片
下一篇:GSON平面映射到其他領域
