Search code examples
androidmultipartimage-captureactivityresultcontracts

How to upload Image to Server from camera Using new Activity Contracts APIs?


I am trying to upload Image to server using Camera Image with new Activity Contracts Method but getting exception like : No such file or directory for my selected Path.

Here is What I had done so far:

private fun takeImage() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
        lifecycleScope.launchWhenStarted {
            getTmpFileUri().let { uri ->
                latestTmpUri = uri
                getImageResult.launch(uri)
            }
        }
    } else {
        requestCameraPermission.launch(Manifest.permission.CAMERA)
    }
}
private fun getTmpFileUri(): Uri {
    val timeStamp: String = SimpleDateFormat("ddMMyyyy", Locale.getDefault()).format(Date())
    val tmpFile = File.createTempFile(timeStamp, ".png", cacheDir).apply {
        createNewFile()
        deleteOnExit()
    }

    return FileProvider.getUriForFile(applicationContext, "${BuildConfig.APPLICATION_ID}.provider", tmpFile)
}
private val getImageResult = registerForActivityResult(ActivityResultContracts.TakePicture()) { isSuccess ->
    if (isSuccess) {
        latestTmpUri?.let {
            //loadImage(it, binding.imgProfile)
            image = File(it.path.toString())
            uploadImage()
        }
    }
}
 if (image != null) {
        profileImage = image.toMultipartBody("profile_image")
    } // and than code for api calls which use profileImage to upload
fun File?.toMultipartBody(name: String): MultipartBody.Part? {
    this ?: return null
    return MultipartBody.Part.createFormData(
        name,
        this.name,
        RequestBody.create(getMimeType().toMediaTypeOrNull(), this)
    )
}

private fun File.getMimeType(): String {
    var type: String? = null
    val url = this.toURI().toURL().toString()
    val extension = url.substringAfterLast(".")
    return when (extension.lowercase(Locale.getDefault())) {
        "jpg", "jpeg", "png" -> "image/$extension"
        "mp4" -> "video/$extension"
        "pdf" -> "application/$extension"
        else -> "image/*"
    }
}

Any Help is Appreciated !!


Solution

  • I had solved this problem as follows:

    Firstly while creating tempFile using private fun getTmpFileUri(): Uri retrieve the absolute path of tempFile and store it in global variable as below:

    private fun getTmpFileUri(): Uri {
        val timeStamp: String = SimpleDateFormat("ddMMyyyy", Locale.getDefault()).format(Date())
        val tmpFile = File.createTempFile(timeStamp, ".png", cacheDir).apply {
            createNewFile()
            deleteOnExit()
        }
        imagePath = tmpFile.absolutePath // store file path in variable
    
        return FileProvider.getUriForFile(applicationContext, "${BuildConfig.APPLICATION_ID}.provider", tmpFile)
    }
    

    now use that path to create image file as below:

     private val getImageResult = registerForActivityResult(ActivityResultContracts.TakePicture()) { isSuccess ->
        if (isSuccess) {
            latestTmpUri?.let {
                image = File(imagePath)
                uploadImage()
            }
        }
    }
    

    Now you can use variable image to upload your image file in uploadImage() method.