开发者社区 问答 正文

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");
}


展开
收起
海边一只船 2020-05-27 10:00:18 906 分享 版权
1 条回答
写回答
取消 提交回答
  • adroid9.0 之后sd卡的路径需要通过getExternalFilesDirs获取,getExternalStorageDirectory获取不到了

    2020-05-27 13:43:14
    赞同 展开评论
问答分类:
问答地址: