1

I want to upload a logo picked in gallery to our api via multipart. I am using multipartFile method from the networking library witch take a File in parameter: uploading a file to server

The File object can take an Uri as constructor.

With the uri received from the gallery return, i can display the image in a ImageView, but the uri or uri.path cause FileNotFoundException when passed in FileConstructor. (internet, read, write permissions are declared in manifest)

there is all code concerning this feature:

//selected image uri
private var imageUri: Uri? = null
set(value) {
    field = value

    field?.let { //value after picking image is : content://com.android.providers.media.documents/document/image%3A25
        uploadImageFile(imageUri= it,
            route = "uploadImageRoute/",
            onProgression = { progress ->
                Log.d("upload", "progress= $progress")
            },
            onSuccess = {
                Log.d("upload", "upload success !")
            },
            onError = { errorMessage ->
                Log.d("upload", "error= $errorMessage")
            })
        }
}


//select image button clicked, go to image gallery to pick an image
override fun onClick(v: View?) {
    val intent = Intent()
    intent.type = "image/*"
    intent.action = Intent.ACTION_GET_CONTENT
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), 0)
}


//comeback from image gallery
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == 0 && resultCode == Activity.RESULT_OK && data != null) {
        val selectedPhotoUri = data.data!!
        imageUri = selectedPhotoUri
    }
}

//prepare request headers
private fun configureHeaders(): MutableMap<String,Any> {
    var headerMap = mutableMapOf<String, Any>()
    headerMap["Content-Type"] = "multipart/form-data"
    headerMap["Accept"] = "application/json, text/plain"
    return headerMap
}

private fun uploadImageFile(imageUri: Uri, route: String, onProgression: (Double) -> Unit, onSuccess: () -> Unit, onError: (String?) -> Unit) {
    val headers = configureHeaders()
        var file = File(imageUri.path)

        AndroidNetworking.upload("https://api.ourDomain.com/$route")
            .addMultipartFile("file", file)
            .addHeaders(headers)
            .build()
            .setUploadProgressListener { uploaded, total ->
                val percentage = 100 * uploaded / total as Double
                onProgression(percentage)
            }
            .getAsJSONObject(object : JSONObjectRequestListener {
                override fun onResponse(response: JSONObject?) {
                    onSuccess()
                }

                override fun onError(anError: ANError?) {
                    onError(anError.toString())
                }
            })
}

How to get the real path to the file to put in parameter of File constructor ?

0 Answers0