书信一封~ 2020-05-24 16:25 采纳率: 100%
浏览 772
已采纳

Android 10 图片上传问题

在6.0 7.0 上传图片没问题。在10.0下就一直报错

Method threw 'java.io.FileNotFoundException' exception

我的代码

public String uploadImage(String url, String imagePath) throws IOException, JSONException {
OkHttpClient okHttpClient = new OkHttpClient();
// Log.d("imagePath", imagePath);
File file = new File(imagePath);
RequestBody image = RequestBody.create(MediaType.parse("image/png"), file);
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("path", "taskPlanApp")
.addFormDataPart("attach", imagePath, image)
.addFormDataPart("thumbnailWidth", "80")
.addFormDataPart("thumbnailHeigth", "80")
.build();
Request request = new Request.Builder()
.url(url)
.post(requestBody)
.build();
Response response = okHttpClient.newCall(request).execute();
JSONObject jsonObject = new JSONObject(response.body().string());
Integer error = jsonObject.getInt("error");
String msg = jsonObject.getString("message");
if (error == 0) {
imgLoadUrl = jsonObject.getString("url");
} else {
Toast.makeText(ConfirmOldManInfo.this, msg, Toast.LENGTH_SHORT).show();
}

return jsonObject.optString("image");
}
  • 写回答

2条回答 默认 最新

  • UFO_SERIESOFSOFT 2020-05-25 12:38
    关注

    先检查能否访问到文件,错误信息来看可能是路径问题

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?