3

I am working on android application in which I am selecting Video files from gallery. Everything is fine but I want to show video files there which are below 5MB, I don't want to show all Videos exceeding 5MB. My code to show Video gallery and onActivityResult is given below:

public void takeVideoFromGallery(){
        Intent intent = new Intent();
        intent.setType("video/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent,"Select Video"),REQUEST_TAKE_GALLERY_VIDEO);

    }
public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == this.RESULT_OK) {

            switch (requestCode) {

case REQUEST_TAKE_GALLERY_VIDEO:
                    if (resultCode == RESULT_OK) {
                        showVideoGallery(data);
                        Toast.makeText(this, "Video saved to:\n" +data.getData(), Toast.LENGTH_LONG).show();

                    } else if (resultCode == RESULT_CANCELED) {
                        Toast.makeText(this, "Video recording cancelled.",Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(this, "Failed to record video",Toast.LENGTH_LONG).show();
                    }
                    break;
}
Usman Khan
  • 3,739
  • 6
  • 41
  • 89
  • you can, however, put the check to compare if the size of file picked is less than 5MB before uploading/using. – Shubham AgaRwal Mar 21 '18 at 16:22
  • @Killer thanks for your quick response. Can you please give me a code snippet as per my above code. It will be very helpful to me. – Usman Khan Mar 21 '18 at 16:23

2 Answers2

3

You can pick video via ACTION_PICKand add extra like EXTRA_SIZE_LIMIT

Reference: https://developer.android.com/reference/android/provider/MediaStore.html#EXTRA_SIZE_LIMIT

note: EXTRA_SIZE_LIMIT information does not work for many cases.

val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, Constant.MAX_VIDEO_SIZE_IN_MB)
intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, MAX_VIDEO_RECORDING_TIME_IN_SEC)
intent.type = "video/*"
startActivityForResult(intent, PICK_VIDEO_GALLERY)

You can compare the file size via (note: better to compress video before using it if size is concerned)

val compressedVideoFile = File(mVideoOutPath)
if (compressedVideoFile.length() > Constant.MAX_VIDEO_FILE_SIZE_IN_BYTES) {
    ToastUtils.shortToast(this@ChatActivity, getString(R.string.error_video_size))
else if (FileUtil.getVideoDuration(this@ChatActivity, compressedVideoFile) > Constant.MAX_VIDEO_FILE_DURATION_IN_MILLIS) {
    ToastUtils.shortToast(this@ChatActivity, getString(R.string.error_video_length))
} else {
    mVideoFilePath = mVideoOutPath
    uploadMedia(Constant.KEY_VIDEO)
}

For retrieving video duration(in case you want to put check on duration as well):

fun getVideoDuration(context: Context, selectedVideoFile: File): Long {
    var videoDuration = java.lang.Long.MAX_VALUE
    try {
        val retriever = MediaMetadataRetriever()
        retriever.setDataSource(context, Uri.fromFile(selectedVideoFile))
        val time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)
        videoDuration = java.lang.Long.parseLong(time)
        retriever.release()
    } catch (e: IllegalArgumentException) {
        e.printStackTrace()
    } catch (e: SecurityException) {
        e.printStackTrace()
    }

    return videoDuration
}
Shubham AgaRwal
  • 4,355
  • 8
  • 41
  • 62
2

There is nothing in ACTION_GET_CONTENT that allows you to provide arbitrary filters, such as a maximum size.

You might see if there is a file picker library that meets your needs. Otherwise, you will need to create this UI yourself, querying the MediaStore and only showing the videos that meet your requirements.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491