Home > front end >  How can upload image from file path to server retrofit kotlin android
How can upload image from file path to server retrofit kotlin android

Time:12-07

How can upload image from file path to server retrofit Kotlin android

api interface

 @Multipart
 @POST("add_passport.php")
 suspend fun addPassport(
    @Part("userid") userid: String,
    @Part("fistname") fistname:String,
    @Part("surname") surname:String,
    @Part("nationality") nationality:String,
    @Part("dof") dof:String,
    @Part("gender") gender:String,
    @Part("age") age:String,
    @Part("sig") sig: String,
    @Part("salt") salt: String ):Response<PassportInsertApiClass>

Repository

suspend fun addPassport(userid: String,fistname:String,surname:String,nationality:String,
                        dof:String,gender:String,age:String,sig:String,salt:String,image:Image):Response<PassportInsertApiClass>{
    return RetrofitInstance.api.addPassport(userid,fistname,surname,nationality,dof,gender,age,sig,salt)
}

view model

fun addPassport(userid: String,fistname:String,surname:String,nationality:String,
                dof:String,gender:String,age:String,sig:String,salt:String,image: Image):
        MutableLiveData<Response<PassportInsertApiClass>>{
    val addPassport = MutableLiveData<Response<PassportInsertApiClass>>()
    viewModelScope.launch {
        addPassport.postValue(repository.addPassport(userid, fistname, surname, nationality, dof, gender, age, sig, salt, image))
    }
    return addPassport
}

fragment

i have saved offline data using SQLite when user what to back up all data upload to server

i don't know about image so didn't write anything :(

CodePudding user response:

Update you API Interface.

@Multipart
@POST("add_passport.php")
 suspend fun addPassport(
    @Part profile_picture: MultipartBody.Part?,
    @Part("userid") userid: String,
    @Part("fistname") fistname:String,
    @Part("surname") surname:String,
    @Part("nationality") nationality:String,
    @Part("dof") dof:String,
    @Part("gender") gender:String,
    @Part("age") age:String,
    @Part("sig") sig: String,
    @Part("salt") salt: String ):Response<PassportInsertApiClass>

Update you Repository

suspend fun addPassport(profile_picture: MultipartBody.Part?, userid: String,fistname:String,surname:String,nationality:String,
                        dof:String,gender:String,age:String,sig:String,salt:String,image:Image):Response<PassportInsertApiClass>{
    return RetrofitInstance.api.addPassport(profile_picture, userid,fistname,surname,nationality,dof,gender,age,sig,salt)
}

Update your view model

fun addPassport(profile_picture: MultipartBody.Part?, userid: String,fistname:String,surname:String,nationality:String,
                dof:String,gender:String,age:String,sig:String,salt:String,image: Image):
        MutableLiveData<Response<PassportInsertApiClass>>{
    val addPassport = MutableLiveData<Response<PassportInsertApiClass>>()
    viewModelScope.launch {
        addPassport.postValue(repository.addPassport(profile_picture, userid, fistname, surname, nationality, dof, gender, age, sig, salt, image))
    }
    return addPassport
}

Update below code in your Activity/Frgament where you are calling viewModel.addPassport() method and send body object in that method for profile_picture.

var body: MultipartBody.Part? = null
        if (!selectedImagePath.equals("", ignoreCase = true)) {
            val file = File(selectedImagePath)
            val reqFile = RequestBody.create(MediaType.parse("image/*"), file)
            body = MultipartBody.Part.createFormData("profile_picture", file.getName(), reqFile)
        }
  • Related