How insert image in room persistence library?

后端 未结 3 2061
轮回少年
轮回少年 2020-12-13 09:23

I am using room persistence library for my android application, Now I have to insert image in my db. I successfully define @Entity for the primitive data type. and also thro

3条回答
  •  时光说笑
    2020-12-13 10:16

    Save the image as a file and save the file path Uri to Room

    As seen in CameraX's image capture use case, when a photo is successfully taken, the File path reference Uri, savedUri, can be retrieved safely.

    Then, the Uri can be converted to a string with savedUri.toString(), and saved to Room.

    • It's important to ensure the Room file reference is also updated if the file is moved or deleted.
    • The image String saved in Room may need to be converted back into a Uri to be displayed with an image library such as Glide with Uri.parse(someString).

    In the CameraX sample, an image path's Uri can safely be obtained in onImageSaved.

    • It would then be saved into Room off of the main thread using Kotlin Coroutines or RxJava, preferably in a ViewModel or somewhere that handles the business logic separate from the view logic.

    Getting Started with CameraX > 5. Implement ImageCapture use case

    private fun takePhoto() {
       // Get a stable reference of the modifiable image capture use case
       val imageCapture = imageCapture ?: return
    
       // Create time-stamped output file to hold the image
       val photoFile = File(
           outputDirectory,
           SimpleDateFormat(FILENAME_FORMAT, Locale.US
           ).format(System.currentTimeMillis()) + ".jpg")
    
       // Create output options object which contains file + metadata
       val outputOptions = ImageCapture.OutputFileOptions.Builder(photoFile).build()
    
       // Set up image capture listener, which is triggered after photo has
       // been taken
       imageCapture.takePicture(
           outputOptions, ContextCompat.getMainExecutor(this), object : ImageCapture.OnImageSavedCallback {
               override fun onError(exc: ImageCaptureException) {
                   Log.e(TAG, "Photo capture failed: ${exc.message}", exc)
               }
    
               override fun onImageSaved(output: ImageCapture.OutputFileResults) {
                   val savedUri = Uri.fromFile(photoFile)
                   val msg = "Photo capture succeeded: $savedUri"
                   Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
                   Log.d(TAG, msg)
               }
           })
    }
    

    This strategy is outlined in Saving image in Room database on Reddit.

    Cloud Storage

    Creating a file for the image and saving the file path in Room covers local storage. In order to ensure the images are saved across multiple devices or when if data cache and data are cleared, a form of Cloud Storage is needed to upload the files to and to download and sync with the local storage.

提交回复
热议问题