我试图在Retrofit中上传多部分多图像上传,但它显示响应失败错误,如 [/ storage / emulated / 0 / Whatsapp / Media / WhatsApp Images / abc.jpg,/ storage / emulated / 0 / Whatsapp / Media / WhatsApp Images / abcd.jpg]开启失败; ENOENT(没有这样的文件或目录)
我按照本教程... https://futurestud.io/tutorials/retrofit-2-how-to-upload-a-dynamic-amount-of-files-to-server
我使用此库进行图像搜索...... https://github.com/myinnos/AwesomeImagePicker
Activity.java:
private void ImageChooser(){
Intent intent = new Intent(this, AlbumSelectActivity.class);
intent.putExtra(ConstantsCustomGallery.INTENT_EXTRA_LIMIT,5); // set limit for image selection
startActivityForResult(intent, ConstantsCustomGallery.REQUEST_CODE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == ConstantsCustomGallery.REQUEST_CODE && resultCode == Activity.RESULT_OK && data != null) {
ArrayList<images> images = data.getParcelableArrayListExtra(ConstantsCustomGallery.INTENT_EXTRA_IMAGES);
ArrayList<String> imagelist = new ArrayList<>();
for (int i = 0; i < images.size(); i++) {
Uri uri = Uri.fromFile(new File(images.get(i).path));
String filePath = getRealPathFromURIPath(uri, WorkerAddPhotosActivity.this);
imagelist.add(filePath);
}
LoadWorkerInquiry(images)
}
}
private String getRealPathFromURIPath(Uri contentURI, Activity activity) {
Cursor cursor = activity.getContentResolver().query(contentURI, null, null, null, null);
if (cursor == null) {
return contentURI.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
}
public void LoadWorkerInquiry(ArrayList<String> image) {
ApiConfig getResponse = AppConfig.getRetrofit().create(ApiConfig.class);
List<MultipartBody.Part> parts = new ArrayList<>();
for (int index = 0; index < image.size(); index++) {
File file = new File(image.toString());
RequestBody surveyBody = RequestBody.create(MediaType.parse("image/*"), file);
parts.add(MultipartBody.Part.createFormData("image", file.getName(), surveyBody));
}
Call<WorkerImageUpload> call = getResponse.uploadImages(parts);
call.enqueue(new Callback<WorkerImageUpload>() {
@Override
public void onResponse(Call<WorkerImageUpload> call, retrofit2.Response<WorkerImageUpload> response) {
WorkerImageUpload serverResponse = response.body();
if (response.isSuccessful()) {
} else {
}
}
@Override
public void onFailure(Call<WorkerImageUpload> call, Throwable t) {
if (t instanceof IOException) {
Error("Timeout",t.getMessage());
}
else if (t instanceof IllegalStateException) {
Error("ConversionError",t.getMessage());
} else {
Error("Error",t.getMessage());
}
}
});
}
Api界面:
@Multipart
@POST("api/***")
Call<WorkerImageUpload> uploadImages(@Part List<MultipartBody.Part> Image);
答案 0 :(得分:0)
我假设您正在尝试访问WhatsApp内部存储,但未经特别授予访问权限这是禁止的。
尝试访问应用存储空间中的文件。此文档非常有用:https://developer.android.com/guide/topics/data/data-storage.html