Reputation: 673
I'm trying to send a photo from the gallery but I have an error indicating image not found
The request does not even go to the server
In fragment
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK) {
when (requestCode) {
REQUEST_SELECT_IMAGE_IN_ALBUM -> {
val contentURI = data!!.data
postServer(contentURI)
}
}
}
}
private fun postServer(contentURI: Uri) {
val MEDIA_TYPE_IMAGE: MediaType = MediaType.parse("image/*")!!
val file = File(contentURI.path)
val requestBody: RequestBody = RequestBody.create(MEDIA_TYPE_IMAGE, file)
mercrediViewModel.uploadImage(enfant, requestBody)
}
In my model
fun uploadImage(enfant: Enfant, requestBody: RequestBody) {
viewModelScope.launch {
val request = mercrediService.uploadImage("****", enfant.id, requestBody)
request.enqueue(object : Callback<ResponseBody> {
override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
Timber.i(" error image" + t)
}
override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
Timber.i(" response image" + response.body())
}
})
}
}
And retrofit service
@Multipart
@POST("api/update/enfant/photo/{id}")
fun uploadImage(
@Header("X-AUTH-TOKEN") token: String?,
@Path("id") id: Int,
@Part("image") image: RequestBody
): Call<ResponseBody>
I have this error
ok thank you, I have change this the error message is the same:
I/MercrediViewModel$uploadImage: zeze error image java.io.FileNotFoundException: /document/image:74 (No such file or directory) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.(FileInputStream.java:146) at okio.Okio.source(Okio.java:168) at okhttp3.RequestBody$3.writeTo(RequestBody.java:119) at okhttp3.MultipartBody.writeOrCountBytes(MultipartBody.java:173) at okhttp3.MultipartBody.writeTo(MultipartBody.java:114) .....
Thank you
Upvotes: 1
Views: 6189
Reputation: 673
Finally my solution
class FileHelper {
fun createFile(realPath: String): File {
return File(realPath)
}
fun createRequestBody(file: File): RequestBody {
val MEDIA_TYPE_IMAGE: MediaType = MediaType.parse("image/*")!!
return RequestBody.create(MEDIA_TYPE_IMAGE, file)
}
fun createPart(file: File, requestBody: RequestBody): MultipartBody.Part {
return MultipartBody.Part.createFormData("image", file.name, requestBody)
}
fun getPathFromURI(context: Context, uri: Uri): String? {
val path: String = uri.path
var realPath: String? = null
val databaseUri: Uri
val selection: String?
val selectionArgs: Array<String>?
if (path.contains("/document/image:")) { // files selected from "Documents"
databaseUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
selection = "_id=?"
selectionArgs = arrayOf(DocumentsContract.getDocumentId(uri).split(":")[1])
} else { // files selected from all other sources, especially on Samsung devices
databaseUri = uri
selection = null
selectionArgs = null
}
try {
val projection = arrayOf(
MediaStore.Images.Media.DATA,
MediaStore.Images.Media._ID,
MediaStore.Images.Media.ORIENTATION,
MediaStore.Images.Media.DATE_TAKEN
) // some example data you can query
val cursor = context.contentResolver.query(
databaseUri,
projection, selection, selectionArgs, null
)
if (cursor.moveToFirst()) {
val columnIndex = cursor.getColumnIndex(projection[0])
realPath = cursor.getString(columnIndex)
}
cursor.close()
} catch (e: Exception) {
Timber.i("zeze get path error " + e.message)
}
return realPath
}
}
@Multipart
@POST("api/update/enfant/photo/{id}")
fun uploadImage(
@Path("id") enfantId: Int,
@Part file: MultipartBody.Part,
@Part("image") requestBody: RequestBody): Call<ResponseBody>
Upvotes: 5