HelloSXQ
2020-05-24 16:25
采纳率: 100%
浏览 700

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
    已采纳

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

    点赞 打赏 评论
  • 熊佳龙 2020-05-26 13:28

    adroid9.0 之后sd卡的路径需要通过getExternalFilesDirs获取,getExternalStorageDirectory获取不到了

    点赞 打赏 评论

相关推荐 更多相似问题