HelloSXQ
HelloSXQ
2020-05-24 16:25

Android 10 图片上传问题

  • android

在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条回答

为你推荐

换一换