Android 如何在改装2 Kotlin的post参数中发送任何类型的文件?

Android 如何在改装2 Kotlin的post参数中发送任何类型的文件?,android,kotlin,retrofit2,multipart,Android,Kotlin,Retrofit2,Multipart,让我分享我的一些代码,我已经实现了在请求中发送图像文件 下面是我的api请求函数: @Multipart @POST("api/order/order_create") fun createOrder( @Header("Authorization") authorization: String?, @Part("category_id") categoryId: RequestBody?, @Part("size") size: RequestBody?, @P

让我分享我的一些代码,我已经实现了在请求中发送图像文件

下面是我的api请求函数:

@Multipart
@POST("api/order/order_create")
fun createOrder(
    @Header("Authorization") authorization: String?,
    @Part("category_id") categoryId: RequestBody?,
    @Part("size") size: RequestBody?,
    @Part("narration") narration: RequestBody?,
    @Part("ref_picture") file: RequestBody?
): Call<OrderCreateResponse>
这里imageFile是通过下面的方式获取的

imageFile = File(Global.getRealPathFromURI(activity!!, imageUri!!))
使用下面的函数获取实际路径

fun getRealPathFromURI(context: Context, contentUri: Uri): String {
        var cursor: Cursor? = null
        try {
            val proj = arrayOf(MediaStore.Images.Media.DATA)
            cursor = context.contentResolver.query(contentUri, proj, null, null, null)
            val column_index = cursor!!.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
            cursor.moveToFirst()
            return cursor.getString(column_index)
        } catch (e: Exception) {
            Log.e(TAG, "getRealPathFromURI Exception : " + e.toString())
            return ""
        } finally {
            if (cursor != null) {
                cursor.close()
            }
        }
    }
通过以上方式发送图像,我无法发送它!请用同样的方法指导我。 提前感谢。

尝试更改
@Part(“参考图片”)文件:RequestBody?


@Part(“参考图片”)文件:MultipartBody.Part?

这样做

// create RequestBody instance from file
RequestBody requestFile = RequestBody.create(MediaType.parse(getContentResolver().getType(fileUri)),file);

// MultipartBody.Part is used to send also the actual file name
MultipartBody.Part body = MultipartBody.Part.createFormData("picture", file.getName(), requestFile);
你也可以检查这个答案

最后一件事是让RequestBody变量

RequestBody.create(MediaType.parse("text/plain"), user_full_name)

最后发送请求:)

您可以这样做:

    var propertyImagePart: MultipartBody.Part? = null
            imageUrl.value?.let {
                val propertyImageFile = File(FILE_PATH)
                val propertyImage: RequestBody = RequestBody.create(MediaType.parse("image/*"), propertyImageFile)
                propertyImagePart =MultipartBody.Part.createFormData("userImage", propertyImageFile.name, propertyImage)
            }

    job = launch {
            try {
                val response = apiServiceWithoutHeader.doUpdateProfile(propertyImagePart,profileRequest.getMultipart()).await()
                stateLiveData.postValue(UserProfileState.SuccessUpdateProfile(response))
            } catch (e: JsonSyntaxException) {
                onException(e)
            } catch (e: JsonParseException) {
                onException(e)
            } catch (e: IOException) {
                onException(e)
            } catch (e: HttpException) {
                onException(e)
            }
        }

如果要将映像发送到服务器,则应使用MultipartBody.part在全局中得到什么。getRealPathFromURI(activity!!,imageUri!!)方法确保检查您的文件不应为null。@MayurPatel是的,先生我已检查了null case的条件@hetsgandhi首先我不是先生,你能分享你的getRealPathFromURI方法代码吗
// image as file
    var body: MultipartBody.Part? = null
    if (!profileImagePath.isNullOrBlank()) {
        val file = File(profileImagePath)
        val inputStream = contentResolver.openInputStream(Uri.fromFile(file))
        val requestFile = RequestBody.create(MediaType.parse("image/jpeg"), getBytes(inputStream))
        body = MultipartBody.Part.createFormData("image", file.name, requestFile)
        Log.d("nama file e cuk", file.name)
    }
RequestBody.create(MediaType.parse("text/plain"), user_full_name)
    var propertyImagePart: MultipartBody.Part? = null
            imageUrl.value?.let {
                val propertyImageFile = File(FILE_PATH)
                val propertyImage: RequestBody = RequestBody.create(MediaType.parse("image/*"), propertyImageFile)
                propertyImagePart =MultipartBody.Part.createFormData("userImage", propertyImageFile.name, propertyImage)
            }

    job = launch {
            try {
                val response = apiServiceWithoutHeader.doUpdateProfile(propertyImagePart,profileRequest.getMultipart()).await()
                stateLiveData.postValue(UserProfileState.SuccessUpdateProfile(response))
            } catch (e: JsonSyntaxException) {
                onException(e)
            } catch (e: JsonParseException) {
                onException(e)
            } catch (e: IOException) {
                onException(e)
            } catch (e: HttpException) {
                onException(e)
            }
        }