我必须使用retrofit2捕获图像并将其上传到服务器 我的API
@Multipart
@POST("photo/")
Call<NetWorkResponse<String>> uploadPhoto(@Header("Authorization") String token,
@Part("category") String category,
@Part MultipartBody.Part photo);
我上传照片的方法
public static void uploadPhoto(String nfToken, String category, File photoFile) {
RequestBody filePart = RequestBody.create(MediaType.parse("image/jpeg"), photoFile);
MultipartBody.Part file = MultipartBody.Part.createFormData("photo", photoFile.getName(), filePart);
Call<NetWorkResponse<String>> call = nfApi.uploadPhoto(nfToken, category, file);
call.enqueue(new Callback<NetWorkResponse<String>>() {
@Override
public void onResponse(Call<NFNetWorkResponse<String>> call, Response<NetWorkResponse<String>> response) {
if (response.isSuccessful()) {
NetWorkResponse<String> netWorkResponse = response.body();
} else {
}
}
@Override
public void onFailure(Call<NetWorkResponse<String>> call, Throwable t) {
}
});
}
我的照片文件Uri:file:///storage/emulated/0/Pictures/Food/IMG_20170731_094856_2128319239.jpg
当我将文件上传到服务器时,我收到错误错误请求,代码400.这可能表明我的参数错误,但我确信这没有任何问题。我觉得这张照片有问题,对于MediaType.parse来说是错误的(&#34; image / jpeg&#34;)
应该是:
File f = new File(mCurrentPhotoPath);
Uri pictureUri = Uri.fromFile(f);
String contentType = getContentResolver().getType(pictureUri);
MediaType.parse(contentType)
但问题是Uri路径是file:// not content:// so contentType为null
我知道将照片上传到服务器时出了什么问题
答案 0 :(得分:0)
取代照片文件网址file:///storage/emulated/0/Pictures/Food/IMG_20170731_094856_2128319239.jpg
传递此/storage/emulated/0/Pictures/Food/IMG_20170731_094856_2128319239.jpg