如何从文件路径上传图像到服务器改造 kotlin android

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

如何从文件路径上传图片到服务器改造Kotlin android

api 界面

 @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>

存储库

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)
}

查看模型

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
}

片段

当用户备份所有数据上传到服务器时,我使用 SQLite 保存了离线数据

我不知道图像所以没有写任何东西:(

更新你API界面。

@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>

更新您的存储库

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)
}

更新您的视图模型

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
}

在调用 viewModel.addPassport() 方法的 Activity/Frgament 中更新以下代码,并在该方法中为 发送 body 对象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)
        }