【问题标题】:upload picture to server android上传图片到服务器android
【发布时间】:2017-07-31 15:31:51
【问题描述】:

我必须使用 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("image/jpeg") 有问题

应该是:

 File f = new File(mCurrentPhotoPath);
 Uri pictureUri = Uri.fromFile(f);
 String contentType = getContentResolver().getType(pictureUri);
 MediaType.parse(contentType)

但问题是 Uri 路径是 file:// 而不是 content:// 所以 contentType 为空

知道我将照片上传到服务器时出了什么问题

【问题讨论】:

    标签: java android


    【解决方案1】:

    而不是照片文件 URL file:///storage/emulated/0/Pictures/Food/IMG_20170731_094856_2128319239.jpg

    传这个/storage/emulated/0/Pictures/Food/IMG_20170731_094856_2128319239.jpg

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-05
      • 2014-07-18
      • 1970-01-01
      • 1970-01-01
      • 2017-05-04
      • 2011-06-01
      相关资源
      最近更新 更多