I am working on android video application where I am recording a Video using Camera Intent and taking a video from gallery. When recording video or after selecting video from gallery I want to know the size of my video which I took. I want to make a logic to show a user a toast message if selected Video size is greater then 5MB. I made the bottom logic which is not working and giving me 0 value where I tried to take the size from URI.
Thanks in advance.
My Logic which is not working
java.net.URI juri = new java.net.URI(uri.toString());
File mediaFile = new File(juri.getPath());
long fileSizeInBytes = mediaFile.length();
long fileSizeInKB = fileSizeInBytes / 1024;
long fileSizeInMB = fileSizeInKB / 1024;
if (fileSizeInMB > 5) {
Toast.makeText(this,"Video files lesser than 5MB are allowed",Toast.LENGTH_LONG).show();
return;
}
This is my code which I am using to get video from Gallery and to record video.
public void takeVideoFromCamera(){
File mediaFile =new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/myvideo.mp4");
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
Uri videoUri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// videoUri = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", mediaFile);
videoUri = FileProvider.getUriForFile(this, "i.am.ce.by.ncy.provider", mediaFile);
} else {
videoUri = Uri.fromFile(mediaFile);
}
intent.putExtra(MediaStore.EXTRA_OUTPUT, videoUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, 5491520L);//5*1048*1048=5MB
intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT,45);
startActivityForResult(intent, VIDEO_CAPTURE);
}
public void takeVideoFromGallery(){
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
long maxVideoSize = 5 * 1024 * 1024; // 10 MB
intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, maxVideoSize);
startActivityForResult(Intent.createChooser(intent,"Select Video"),REQUEST_TAKE_GALLERY_VIDEO);
}
onActivityResult code
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == this.RESULT_OK) {
switch (requestCode) {
case VIDEO_CAPTURE:
if (resultCode == RESULT_OK) {
showVideoImage(data.getData());
// Here I want to know what is the size of my Video File
}
break;
case REQUEST_TAKE_GALLERY_VIDEO:
if (resultCode == RESULT_OK) {
showVideoGallery(data);
// Here I want to know what is the size of my Video File
}
break;
}