Upload Progress for video upload in android SDK not working

Comments

17 comments

  • Avatar
    Abhishek Shukla

    Any Update on this issue!

    0
    Comment actions Permalink
  • Avatar
    Yakir Perlin

    Hi Abhishek,

    In order to understand the issue better, I need some more information. 

    Could you please share with me the trimmed video?

    Looking forward to your response.

     

    Thank you, 

    Yakir

    0
    Comment actions Permalink
  • Avatar
    Abhishek Shukla

    The trimmed video gets successfully uploaded, but the onProgress API does not work while uploading the trimmed video.

    Here is the link to uploaded trimmed video: https://res.cloudinary.com/bonita/video/upload/v1518354522/sample_video.mp4

     

    Thanks

    0
    Comment actions Permalink
  • Avatar
    Yakir Perlin

    Hi Abhishek,

    I tried to upload this video with our Android SDK and it seems to work fine for me, including the progress issue.

    Could you please share with me the code you are using to perform the upload?

    Best,

    Yakir

     

    0
    Comment actions Permalink
  • Avatar
    Abhishek Shukla
    public class AddCloudinaryVideo extends AppCompatActivity {

    private static final int SELECT_VIDEO = 2;
    private static final int TRIM_VIDEO = 3;
    private static final int REQUEST_VIDEO_TRIMMER = 0x01;
    private static final int REQUEST_STORAGE_READ_ACCESS_PERMISSION = 101;
    static final String EXTRA_VIDEO_PATH = "EXTRA_VIDEO_PATH";
    private static RingProgressBar ringProgressBar;
    private static ProgressDialog mProgressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_cloudinary_video);

    //ringProgressBar = findViewById(R.id.progress_bar_2);
    //ringProgressBar.setProgress(0);

    mProgressDialog = new ProgressDialog(this);
    mProgressDialog.setMessage("Uploading Video...");
    mProgressDialog.setCancelable(false);
    mProgressDialog.setMax(100);
    mProgressDialog.setIndeterminate(false);
    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

    pickFromGallery();

    /*
    Intent intent = new Intent();
    intent.setType("video/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(intent, SELECT_VIDEO);
    */
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
    if (requestCode == REQUEST_VIDEO_TRIMMER) {
    final Uri selectedUri = data.getData();
    if (selectedUri != null) {
    long duration = getVideoDuration(selectedUri);
    //converting duration in mins
    //duration = duration/(1000 * 60);
    Log.i("uri",selectedUri.getPath());
    Log.i("duration",String.valueOf(duration));

    long size = getVideoSize(selectedUri);
    Log.i("video size", String.valueOf(size));

    if (duration > 2 || size > 17) {
    startTrimActivity(selectedUri);
    } else {
    uploadVideoCloudinary(selectedUri);
    }
    } else {
    Toast.makeText(AddCloudinaryVideo.this, R.string.toast_cannot_retrieve_selected_video, Toast.LENGTH_SHORT).show();
    }
    } else if (requestCode == TRIM_VIDEO) {
    final String new_path = data.getStringExtra("TRIMMED_VIDEO_URI");
    File file = new File(new_path);
    Uri new_uri = Uri.fromFile(file);
    if (new_uri != null) {
    Log.i("new_uri", new_uri.toString());
    //long size = getVideoSize(new_uri);
    long size = file.length();
    size = size /(1024 * 1024);
    if (size % (1024 * 1024) != 0) {
    size += 1;
    }
    Log.i("video size", String.valueOf(size));
    if (size > 17) {
    Toast.makeText(this, "Video size too large, should be less than 17 MB", Toast.LENGTH_LONG).show();

    } else {
    uploadVideoCloudinary(new_uri);
    }
    }
    }
    }
    }

    private long getVideoDuration(Uri videoUri) {
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    //use one of overloaded setDataSource() functions to set your data source
    retriever.setDataSource(this, videoUri);
    String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
    long timeInMillisec = Long.parseLong(time);
    retriever.release();
    long timeInMin = timeInMillisec/(1000 * 60);
    if (timeInMillisec % (1000 * 60) != 0) {
    timeInMin += 1;
    }
    return timeInMin;
    }

    private long getVideoSize(Uri videoUri) {
    String[] mediaColumns = {MediaStore.Video.Media.SIZE};
    Cursor cursor = getApplicationContext().getContentResolver().query(videoUri, mediaColumns, null, null, null);
    if (cursor != null) {
    cursor.moveToFirst();
    int sizeColInd = cursor.getColumnIndex(mediaColumns[0]);
    long fileSize = cursor.getLong(sizeColInd);
    cursor.close();
    long fileSizeInMB = fileSize / (1024 * 1024);
    if (fileSize % (1024 * 1024) != 0) {
    fileSizeInMB += 1;
    }
    return fileSizeInMB;
    }
    return -1;
    }

    private void startTrimActivity(@NonNull Uri uri) {
    Intent intent = new Intent(this, TrimVideo.class);
    intent.putExtra(EXTRA_VIDEO_PATH, FileUtils.getPath(this, uri));
    //startActivity(intent);
    startActivityForResult(intent, TRIM_VIDEO);
    }


    private void pickFromGallery() {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
    requestPermission(Manifest.permission.READ_EXTERNAL_STORAGE, getString(R.string.permission_read_storage_rationale), REQUEST_STORAGE_READ_ACCESS_PERMISSION);
    } else {
    Intent intent = new Intent();
    intent.setTypeAndNormalize("video/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    startActivityForResult(Intent.createChooser(intent, getString(R.string.label_select_video)), REQUEST_VIDEO_TRIMMER);
    }
    }

    private void uploadVideoCloudinary(Uri videoUri) {
    String requestId = MediaManager.get().upload(videoUri)
    .option("public_id", "sample_video")
    .option("resource_type", "video")
    .unsigned("sample_preset")
    .callback(new UploadCallback() {
    @Override
    public void onStart(String requestId) {
    //ringProgressBar.setVisibility(View.VISIBLE);
    mProgressDialog.show();
    }

    @Override
    public void onProgress(String requestId, long bytes, long totalBytes) {
    double progress = (double) bytes/totalBytes;
    progress = progress * 100;
    Log.i("progress", String.valueOf(progress));
    //ringProgressBar.setProgress((int)progress);
    mProgressDialog.setProgress((int)progress);
    }

    @Override
    public void onSuccess(String requestId, Map resultData) {
    Toast.makeText(getApplicationContext(), "Video Uploaded Successfully !",Toast.LENGTH_LONG).show();
    //ringProgressBar.setVisibility(View.INVISIBLE);
    mProgressDialog.dismiss();
    }

    @Override
    public void onError(String requestId, ErrorInfo error) {
    Toast.makeText(getApplicationContext(), "Failed to Upload Video !",Toast.LENGTH_LONG).show();
    //ringProgressBar.setVisibility(View.INVISIBLE);
    mProgressDialog.dismiss();
    }

    @Override
    public void onReschedule(String requestId, ErrorInfo error) {

    }
    })
    .dispatch();
    }

    /**
    * Requests given permission.
    * If the permission has been denied previously, a Dialog will prompt the user to grant the
    * permission, otherwise it is requested directly.
    */
    private void requestPermission(final String permission, String rationale, final int requestCode) {
    if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(getString(R.string.permission_title_rationale));
    builder.setMessage(rationale);
    builder.setPositiveButton(getString(R.string.label_ok), new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
    ActivityCompat.requestPermissions(AddCloudinaryVideo.this, new String[]{permission}, requestCode);
    }
    });
    builder.setNegativeButton(getString(R.string.label_cancel), null);
    builder.show();
    } else {
    ActivityCompat.requestPermissions(this, new String[]{permission}, requestCode);
    }
    }

    /**
    * Callback received when a permissions request has been completed.
    */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
    case REQUEST_STORAGE_READ_ACCESS_PERMISSION:
    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    pickFromGallery();
    }
    break;
    default:
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
    }
    }

    Here is the Code for the Upload Activity which i am using to upload video to cloudinary.
    The issue is coming for trimmed videos after calling trim activity which is a third party lib for trimming videos.
    feel free to ask me for any issue you see in the code.

    Thanks
    0
    Comment actions Permalink
  • Avatar
    Yakir Perlin

    Hi,

    Could you please share with me the trim library you are using?

    0
    Comment actions Permalink
  • Avatar
    Abhishek Shukla

    Hi,

    this is the link to the trim lib I am using.

    https://github.com/titansgroup/k4l-video-trimmer 

     

    thanks

    0
    Comment actions Permalink
  • Avatar
    Yakir Perlin

    Hi, 

     

    I'm sorry for the late response.

    forked the original lib in order to test this it with cloudinary, and it seems to works (theonProgress issue).

    Do you have any public repo for your project?

    Thanks,

    Yakir

     

    0
    Comment actions Permalink
  • Avatar
    Abhishek Shukla

    Hi Yakir, 

    Here is the link to the public repo for my project.

    https://bitbucket.org/abhi2196/sample-app 

    Thanks,

    Abhishek 

    0
    Comment actions Permalink
  • Avatar
    Yakir Perlin

    Hi, 

    Thanks for sharing this.

    I saw that for regular upload it works with :

     MediaManager.get().upload(videoUri)

    And for the trimmed video, you need to change to this ( in order to make the onProgress works):

     MediaManager.get().upload(videoUri.getPath())

    So for now, please add some logic here to select the right source to upload.

    I will check this issue internally with the team.

    Please let me know if it works for you

    Best,

    Yakir

     

    0
    Comment actions Permalink
  • Avatar
    Abhishek Shukla

    Hi,

    I tried the logic for trimmed video and it is working.

    But can you tell me the reason why I need to convert the uri to uri.getPath().

    Anyways thanks for your response. It did solve my issue finally.

     

    Thanks,

    Abhishek

    0
    Comment actions Permalink
  • Avatar
    Yakir Perlin

    Hi, 

    The problem is that we can not always detect the size of the file while using URI, you should prefer working with a file path, if possible.

    By the way, this is the method that trying to detect to source size in our SDK-

    https://github.com/cloudinary/cloudinary_android/blob/8e3b3185961ca093eb3ecf3faaffe5f99e8a8dea/lib/src/main/java/com/cloudinary/android/payload/LocalUriPayload.java#L47

    We will check if we can do something about it in our next versions. 

    Best, 

    Yakir

     

    0
    Comment actions Permalink
  • Avatar
    manassur

    it's funny how after about two years, i have this exact issue. in my own case, the video is not even uploading. it doesn't show progress either

    0
    Comment actions Permalink
  • Avatar
    Daniel Mendoza

    @manassur

    If the video is not uploading then maybe there is another issue happening. Would you be able to share details of the error you are receiving when performing the upload and the code being used to do the upload? If you wish to keep these details confidential please open a support ticket: https://support.cloudinary.com/hc/en-us/requests/new

    0
    Comment actions Permalink
  • Avatar
    manassur

    hello, i have created a github repository for the project

    0
    Comment actions Permalink
  • Avatar
    manassur

    https://github.com/manassur/mexpress

    0
    Comment actions Permalink
  • Avatar
    Michal Kuperman

    Hi Manassur,

    By default, the resource type is set to image.  Can you please add "resource_type",  "auto", or "resource_type",  "video" to your upload options?

    Like this:

    .option("resource_type", "video")

    Let me know if this solves your issue,

    Best,

    Michal

    0
    Comment actions Permalink

Please sign in to leave a comment.