Saving to Google Photos stream using CameraX

Issue

I’m trying to save photos to the Google Photos stream so they automatically upload to the cloud on Android. In Kotlin, how does one use the CameraX imageCapture.takePicture() API to save to the device’s photos such that they upload to Google Photos, in the same way that screenshots & photos taken with the regular photo app do?

    public fun capturePhoto() {

        // Get a stable reference of the modifiable image capture use case
        val imageCapture = imageCaptureUseCase ?: return

        // Create time stamped name and MediaStore entry.
        val name = SimpleDateFormat(FILENAME_FORMAT, Locale.US).format(System.currentTimeMillis())

//        val oFile = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "HeyThisISJayuir.jpg")

        val contentValues = ContentValues()
            .apply {
                put(MediaStore.MediaColumns.DISPLAY_NAME, name)
                put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg")
                if(Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
                    put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/CameraX-photos")
                }
            }

        // Create output options object which contains file + metadata
        val outputOptions = ImageCapture.OutputFileOptions
            .Builder(
                oContext.contentResolver,
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                contentValues)
            .build()

        // Set up image capture listener, which is triggered after photo has
        // been taken
        imageCapture.takePicture(
            outputOptions,
            ContextCompat.getMainExecutor(oContext),
            object : ImageCapture.OnImageSavedCallback {
                override fun onError(exc: ImageCaptureException) {
                    val msg = "Photo capture FAILED: ${exc.message}"
                    Toast.makeText(oContext, msg, Toast.LENGTH_SHORT).show()
                    Log.e("takePhoto()", "Photo capture failed: ${exc.message}", exc)
                }

                override fun onImageSaved(output: ImageCapture.OutputFileResults){
                    val msg = "Photo capture succeeded: ${output.savedUri}"
                    Toast.makeText(oContext, msg, Toast.LENGTH_SHORT).show()
                    Log.d("takePhoto()", msg)
                }
            }
        )
    }

Solution

I solved this.

One needs to use OAuth2 to get a token to upload photos to the Google Photos API endpoint. This needs to be done after saving to the local media store (as described above).

There is no way that I could find to get the device to do it for you; one needs to get the user to re-auth with Google from within an app first.

More info here:
https://developers.google.com/photos/library/guides/upload-media

And info on creating an album is here:
https://developers.google.com/photos/library/reference/rest/v1/albums/create

.\p

Answered By – AP on CompSciFutures

Answer Checked By – Jay B. (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *