Why Does PDF Is Not Able to Upload in PHP API for Android Pie, Q and R Using Retrofit 2 in Android/Java? [Not Solved]

Unable to upload video using Retrofit, possible fix?

The issue is that you have a content URI and not a file path. When you pass that to the File constructor, it throws a FileNotFoundException that you silently ignore with

`catch (Exception e){}`

Instead of using a file, you can get a file descriptor to the data using --

ParcelFileDescriptor fd = getActivity().getContentResolver().openFileDescriptor(uri, "r");

and use that to construct your RequestBody. There is no out of the box method to create one from the descriptor, but it is fairly easy to write a routine to do it --

public static RequestBody createBody(final MediaType contentType, final ParcelFileDescriptor fd) {
if (fd == null) throw new NullPointerException("content == null");

return new RequestBody() {
@Override public MediaType contentType() {
return contentType;
}

@Override public long contentLength() {
return fd.getStatSize();
}

@Override public void writeTo(BufferedSink sink) throws IOException {
Source source = null;
try {
source = Okio.source(new ParcelFileDescriptor.AutoCloseInputStream(fd));
sink.writeAll(source);
} finally {
Util.closeQuietly(source);
}
}
};
}

then use it like --

RequestBody video = RequestBody.createBody(MediaType.parse("video/mp4"), fd);

I also noticed that you are hardcoding the media type, you can get the type from the content resolver as well with a call to getType

Retrofit Uploading multiple images to a single key

We can use MultipartBody.Part array to upload an array of images to a single key.
Here is the solution

WebServicesAPI

@Multipart
@POST(WebServices.UPLOAD_SURVEY)
Call<UploadSurveyResponseModel> uploadSurvey(@Part MultipartBody.Part[] surveyImage,
@Part MultipartBody.Part propertyImage,
@Part("DRA") RequestBody dra);

Here is the method for uploading the files.

private void requestUploadSurvey () {
File propertyImageFile = new File(surveyModel.getPropertyImagePath());
RequestBody propertyImage = RequestBody.create(MediaType.parse("image/*"),
propertyImageFile);
MultipartBody.Part propertyImagePart = MultipartBody.Part.createFormData("PropertyImage",
propertyImageFile.getName(),
propertyImage);

MultipartBody.Part[] surveyImagesParts = new MultipartBody.Part[surveyModel.getPicturesList()
.size()];

for (int index = 0; index <
surveyModel.getPicturesList()
.size(); index++) {
Log.d(TAG,
"requestUploadSurvey: survey image " +
index +
" " +
surveyModel.getPicturesList()
.get(index)
.getImagePath());
File file = new File(surveyModel.getPicturesList()
.get(index)
.getImagePath());
RequestBody surveyBody = RequestBody.create(MediaType.parse("image/*"),
file);
surveyImagesParts[index] = MultipartBody.Part.createFormData("SurveyImage",
file.getName(),
surveyBody);
}

final WebServicesAPI webServicesAPI = RetrofitManager.getInstance()
.getRetrofit()
.create(WebServicesAPI.class);
Call<UploadSurveyResponseModel> surveyResponse = null;
if (surveyImagesParts != null) {
surveyResponse = webServicesAPI.uploadSurvey(surveyImagesParts,
propertyImagePart,
draBody);
}
surveyResponse.enqueue(this);
}


Related Topics



Leave a reply



Submit