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
What's Java Hybrid - Applet + Application
Android Jdbc Not Working: Classnotfoundexception on Driver
How to Set Addsnapshotlistener and Remove in Populateviewholder in Recyclerview Item
Understand the R Class in Android
Select Random Document from Firestore
Wait Until Tomcat Finishes Starting Up
Sending Command to Java -Jar Using Stdin via /Proc/{Pid}/Fd/0
Fill an Array With Random Numbers
Multiple Row Selection in Jtable
How to Access Static Resources When Mapping a Global Front Controller Servlet on /*
Access Viewpager Fragment Method from Activity
Recyclerview Horizontal Scrolling to Left
How to Use the Legacy Apache Http Client on Android Marshmallow
Android: Difference Between Onintercepttouchevent and Dispatchtouchevent