我如何使用 okhttp 在 Android Q 中通过 Uri 而不是文件上传照片

How I can upload photos by Uri instead of File in Android Q using okhttp

我正在使用 Okhttp (com.squareup.okhttp3:okhttp:3.12.10) 使用此代码将照片上传到服务器

MultipartBody.Builder builder = new MultipartBody.Builder()
                    .setType(MultipartBody.FORM)
                    .addFormDataPart("imageType", Img_tag)
                    .addFormDataPart("created", Calendar.getInstance().getTime() + "")
                    .addFormDataPart("comment", comment + ""))

            for (int i = 0; i < IMG_PATHs.size(); count = ++i) {
                String IMG_Name = IMG_PATHs.get(i).substring(IMG_PATHs.get(i).lastIndexOf("/") + 1);
                String IMG_Extention = IMG_Name.substring((IMG_Name.lastIndexOf(".") + 1));

                builder.addFormDataPart("imageFile" + i, IMG_Name,
                        RequestBody.create(MEDIA_TYPE, new File(IMG_PATHs.get(i))))
                        .addFormDataPart("ext" + i, IMG_Extention);
            }
            builder.addFormDataPart("count", count + "");
            MultipartBody requestBody = builder.build();
            Request request = new Request.Builder()
                    .url(PTHOT_UPLOAD_PATH)
                    .post(requestBody)
                    .build();

现在,在我更新到 android 中的范围存储后,我有一个 Uri 列表,我应该如何更改这个上传照片的请求?

发表评论后解决:InputStreamRequestBody。

https://github.com/square/okhttp/issues/3585#issuecomment-327319196