我從用戶那里得到一個影像,該影像被轉換為??位圖。然后我將位圖轉換為一個位元組陣列,并通過 JSON 發送它以存盤在資料庫中。然后,當用戶開始特定活動時,我希望從資料庫中檢索影像并顯示給用戶。
我的問題是我得到的位圖似乎是對設備上某些記憶體的參考,android.graphics.Bitmap@324a72b盡管我選擇了相同的影像,但每次運行應用程式時都會發生變化。我希望能夠檢索實際位圖,以便將其存盤在資料庫中。由于它是一個較小的專案,我也沒有用作存盤影像的網路服務器。
b.buttonNewItemUpImg.setOnClickListener {
val openGalleryIntent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(openGalleryIntent, ResultLoadImage)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == ResultLoadImage){
if (resultCode == Activity.RESULT_OK){
var temp = MediaStore.Images.Media.getBitmap(this.contentResolver, data!!.getData())
bitmap = getResizedBitmap(temp!!, maxImageSize)
b.imageView.setImageURI(data!!.getData())
}
}
}
getResizedBitmap() 函式只是使影像變小。
uj5u.com熱心網友回復:
如https://stackoverflow.com/a/57576381/6359367 中所述
用這個
var temp = ImageDecoder.createSource(this.getContentResolver(), data!!.getData())
bitmap = ImageDecoder.decodeBitmap(temp);
請注意這段代碼是 java 我試圖將它改編為 kotlin 你可能需要稍微調整一下
uj5u.com熱心網友回復:
不是將整個影像存盤在資料庫中,您應該將影像復制到應用程式的本地存盤,然后將這些復制影像的路徑存盤到資料庫中,您可以稍后使用它來檢索影像。
獲取所選影像后將影像復制到應用程式的本地存盤 uri
//creating an image file in the app's local storage
val dir = requireActivity().getDir(FOLDER_TO_SAVE, Context.MODE_PRIVATE).path
val copiedImageFile = File(dir, UUID.randomUUID().toString() ".jpg")
val copiedImageUri = Uri.fromFile(copiedImageFile)
//copying selected image to the local storage
data!!.getData().path?.let {
File(it).copyTo(copiedImageFile)
}
獲取上述復制影像的路徑,使用其 uri
copiedImageUri.path.?.let{
val copiedImagePath = "file://$it"
//now save this path to your database
}
使用此保存的影像路徑檢索任何片段或活動中的影像。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/362387.html
上一篇:僅使用數字格式化文本框
