Unable to upload video using Retrofit, possible fix?

1.4k Views Asked by At

I am trying to upload video to server using Retrofit as multi part form data , And I am using Retrofit version: 2.02

compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'

This is the code to the API client class:

ApiClient.java

    public class ApiClient {

    public static final String BASE_URL = "https://api.sproutvideo.com";
    private static Retrofit retrofit = null;


    public static Retrofit getClient() {
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

This is the code to API interface:

ApiInterface.java

    public interface ApiInterface {
    @Multipart
    @POST("/v1/videos")
    Call<SproutReply> pushVideo (@Header("SproutVideo-Api-Key") String key, @Part("file\"; filename=\"pp.mp4") RequestBody file);
}

And my requirement is to upload the video picked using Intent.ACTION_PICK.

And this is how I invoke the API:

 private void makeRequest(Uri uri)
{


   try {
       String path = uri.toString();
       Log.e("PATH", path);
       URI ss = new URI(uri.toString());
       file = new File(ss);

   }
   catch (Exception e){}


    RequestBody video = RequestBody.create(MediaType.parse("video/mp4"),file);
    //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    ApiInterface apiService =
            ApiClient.getClient().create(ApiInterface.class);

    Call<SproutReply> call = apiService.pushVideo(KEY,video);
    call.enqueue(new Callback<SproutReply>()
    {
        @Override
        public void onResponse(Call<SproutReply> call, Response<SproutReply> response)
        {
            try
            {
                Log.e("TAG",""+response.body().toString());




            }
            catch (Exception e)
            {
                Toast.makeText(getActivity(), "Check data connection", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(Call<SproutReply> call, Throwable t)
        {
            // Log error here since request failed
            Log.e("FAILURE", t.toString());
        }
    });


}

The call results in a NullPointerException as content == null at this line:

RequestBody video = RequestBody.create(MediaType.parse("video/mp4"),file);

However in this line when I log the Uri( Log.e("PATH", path); ) as String I obtain the value as :

content://com.android.providers.media.documents/document/video%3A75406

And using this Uri I am able to play video in VideoView as well, however in Retrofit it seems to crash, what is possibly causing this and how to fix?

Here is the logcat as well:

java.lang.NullPointerException: content == null
                                                             at okhttp3.RequestBody.create(RequestBody.java:103)
                                                             at com.example.demovid.UploadFragment.makeRequest(UploadFragment.java:91)
                                                             at com.example.demovid.UploadFragment.onEvent(UploadFragment.java:133)
                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                             at org.greenrobot.eventbus.EventBus.invokeSubscriber(EventBus.java:485)
                                                             at org.greenrobot.eventbus.EventBus.postToSubscription(EventBus.java:416)
                                                             at org.greenrobot.eventbus.EventBus.postSingleEventForEventType(EventBus.java:397)
                                                             at org.greenrobot.eventbus.EventBus.postSingleEvent(EventBus.java:370)
                                                             at org.greenrobot.eventbus.EventBus.post(EventBus.java:251)
                                                             at com.example.demovid.MainActivity.onActivityResult(MainActivity.java:67)
                                                             at android.app.Activity.dispatchActivityResult(Activity.java:6456)
                                                             at android.app.ActivityThread.deliverResults(ActivityThread.java:3729)
                                                             at android.app.ActivityThread.handleSendResult(ActivityThread.java:3776)
                                                             at android.app.ActivityThread.-wrap16(ActivityThread.java)
                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412)
                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                             at android.os.Looper.loop(Looper.java:148)
                                                             at android.app.ActivityThread.main(ActivityThread.java:5461)
                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
1

There are 1 best solutions below

9
On BEST ANSWER

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