我获取活动结果的文件。或者我应该说内容uri版本。
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK && requestCode==0){
Log.d("uri data",""+data.getData());
selectedFileUri = data.getData();
我的问题是如何通过okhttp3方法发送此文件?
okhttp 3方法如下
multipart.addFilePart("data[User][user_picture]", selectedFileUri);
感谢您的时间。
答案 0 :(得分:2)
OkHttpClient client = new OkHttpClient();
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("image", "your file name.png",
RequestBody.create(MEDIA_TYPE_PNG, new File("your absolute file path")))
.build();
Request request = new Request.Builder()
.url("Your url")
.post(requestBody)
.build();
//Check the response
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
System.out.println(response.body().string());
}
浏览此博客,了解URI
How to Consume Content From a Uri
或强>