Android从图库中获取视频的文件大小

时间:2016-12-25 15:27:40

标签: android video filesize

我必须将视频从图库上传到VideoView。但我想以字节为单位获取视频的大小。请帮我 !! 这是我的代码:

 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();
                    }
                }
            }

2 个答案:

答案 0 :(得分:7)

您只需要创建一个新的File对象,例如......

    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();

这给出了文件的大小,而不是图像

答案 1 :(得分:0)

拥有一个URI,你可以这样做:

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();