Retrofit 2 Multipart image upload with data

后端 未结 5 714
陌清茗
陌清茗 2020-12-30 16:37

Hello everyone I want to post image and other data through Retrofit2. I am sending data with one image.

All the other info is storing but my image is

5条回答
  •  情深已故
    2020-12-30 17:02

    get Image like this

    Uri mImageUri = data.getData();
    
    // Get the cursor
    Cursor cursor = getContentResolver().query(mImageUri, 
        filePathColumn, null, null, null);
    // Move to first row
    cursor.moveToFirst();
    
    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    imageURI = cursor.getString(columnIndex);
    cursor.close(); 
    
    File file = new File(mImageUri.getPath())
    RequestBody reqFile = RequestBody.create(okhttp3.MediaType.parse("image/*"), file);
    MultipartBody.Part body = MultipartBody.Part.createFormData("image",
        file.getName(), reqFile);
    

提交回复
热议问题