2

I am trying to upload a video to a server using okhttp client and although the onsuccess method is reached the video is not playable on the website. My question is, am I uploading the video correctly using the storage path or am I missing something?

public void videoUploadTo(String url, String storageUrl) {
    OkHttpClient client = new OkHttpClient();
    File videoFile = new File(storageUrl);
    Uri videoUri = Uri.fromFile(videoFile);

    String content_type = getMimeType(videoFile.getPath());
    RequestBody fileBody = RequestBody.create(MediaType.parse(content_type), videoFile);

    RequestBody requestBody = new MultipartBody.Builder()
            .setType(MultipartBody.FORM)
            .addFormDataPart("type", content_type)
            .addFormDataPart("uploaded_video", storageUrl.substring(storageUrl.lastIndexOf("/") + 1), fileBody)
            .build();

    Request request = new Request.Builder()
            .url(siteUrl + url)
            .put(requestBody)
            .build();

    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            Log.d("onfailure video", e.getMessage());
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            Log.d("onResponse video", response.message());
        }
    });
}
Shashanth
  • 4,995
  • 7
  • 41
  • 51
  • What's the error on the server side? – OneCricketeer Nov 29 '17 at 06:40
  • 1
    The error when trying to play the video on the website is : DOMException: The element has no supported sources. – AndroidLearner Nov 29 '17 at 06:56
  • Can you add your relevant web server code? Can you download the file to a computer with a normal video player and play it? – OneCricketeer Nov 29 '17 at 06:58
  • I cannot put web server code at the moment. But when downloading the file to my computer I still cannot play it. My hunch is that I am not creating the file correctly? – AndroidLearner Nov 29 '17 at 18:06
  • Okay, then does the file play on the Android device? What value did you give to `storageUrl`? Something like this? https://stackoverflow.com/questions/8370665/read-file-from-external-storage – OneCricketeer Nov 29 '17 at 21:34
  • 1
    Yes I did. I was able to solve this. Don't know why but when I added to the headers the content type of octet-stream the video seems to have been sent and playing on the website. – AndroidLearner Nov 30 '17 at 00:32
  • @AndroidLearner please add ur correct code as Answer – M. Usman Khan Jul 21 '20 at 17:30

1 Answers1

0

You can do something like this to create a Requestbody out of a FileInputStream:

        final InputStream inputStream = new FileInputStream(videoFile);

        final long fileSize;
        fileSize = videoFile.length();

        RequestBody requestBody = new RequestBody() {
            @Override
            public MediaType contentType() {
                return MediaType.parse("video");
            }

            @Override
            public long contentLength() throws IOException {
                return fileSize;
            }

            @Override
            public void writeTo(BufferedSink sink) throws IOException {
                byte [] buffer = new byte[65536];
                int bytesRead = 0;
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    sink.write(buffer, 0, bytesRead);
                }
            }
        };

And then you can add that body as a part to your MultipartBody like this:

.addFormDataPart("video_file", videoFile.getName(), requestBody);
will_g
  • 1
  • 1