Reputation: 41
I m trying to upload Multipart Multiple Image Upload in Retrofit,but it shows response failure error like that [/storage/emulated/0/Whatsapp/Media/WhatsApp Images/abc.jpg,/storage/emulated/0/Whatsapp/Media/WhatsApp Images/abcd.jpg] open failed; ENOENT (No such file or directory)
I followed this tutorial...https://futurestud.io/tutorials/retrofit-2-how-to-upload-a-dynamic-amount-of-files-to-server
I used this Library for Image Picker...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 Interface :
@Multipart
@POST("api/***")
Call<WorkerImageUpload> uploadImages(@Part List<MultipartBody.Part> Image);
Upvotes: 0
Views: 815
Reputation: 901
I assume you are trying to access WhatsApp internal storage which is forbidden without specifically granted access.
Try to access files in your app storage. This documentation will be useful: https://developer.android.com/guide/topics/data/data-storage.html
Upvotes: 0