Android get file size of video from gallery
Asked Answered
A

2

8

I have to upload a video from gallery to VideoView. But i want to get the size of the video in bytes. Please help me !! This is my code:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent=new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    intent.setType("video/*");
                    startActivityForResult(intent,1);
}
            @Override
            protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                if(requestCode==1&&resultCode== Activity.RESULT_OK){
                    try{
                        String path=data.getData().toString();
                        videoView.setVideoPath(path);
                        videoView.requestFocus();
                        videoView.start();
                       }
                    catch (Exception ex){
                        ex.printStackTrace();
                    }
                }
            }
Airspeed answered 25/12, 2016 at 15:27 Comment(0)
H
12

You just need to create a new File object such as...

    String filepath = Environment.getExternalStorageDirectory() + "/file.mp4";
    File file = new File(filepath);
    long length = file.length();
    length = length/1024;
    Toast.makeText(getActivity(), "Video size:"+length+"KB",
    Toast.LENGTH_LONG).show();

this gives the size of the file, not the image

Hose answered 26/12, 2016 at 4:24 Comment(0)
A
1

Having an URI you can do it like this:

String[] mediaColumns = {MediaStore.Video.Media.SIZE};
Cursor cursor = getContext().getContentResolver().query(videoUri, mediaColumns, null, null, null);
cursor.moveToFirst();
int sizeColInd = cursor.getColumnIndex(mediaColumns[0]);
long fileSize = cursor.getLong(sizeColInd);
cursor.close();
Amylaceous answered 22/9, 2017 at 10:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.