在Android中使用URI获取文件大小 [英] Get file size using URI in android

查看:869
本文介绍了在Android中使用URI获取文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用android video应用程序,在该应用程序中,我使用Camera Intent录制视频并从图库中获取视频.录制视频时或从图库中选择视频后,我想知道我拍摄的视频的大小.如果选择的视频大小大于5MB,我想做出一个向用户显示敬酒消息的逻辑. 我做了底层逻辑,使它无法正常工作,并在尝试从URI中获取大小的地方给了0值.

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.

谢谢.

我的逻辑不起作用

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

这是我用来从Gallery获取视频并录制视频的代码.

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代码

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

推荐答案

java.net.URI juri = new java.net.URI(uri.toString());
File mediaFile = new File(juri.getPath());

Uri不是File.

showVideoImage(data.getData());

ACTION_VIDEO_CAPTURE不返回Uri .

ACTION_VIDEO_CAPTURE does not return a Uri.

此外,您已经知道文件的位置.您在takeVideoFromCamera()中创建一个File对象,并将其用于EXTRA_OUTPUT.按住该File对象(并将其保存为已保存的实例状态Bundle),然后使用该对象找出生成的视频的大小.

Moreover, you already know where the file is. You create a File object in takeVideoFromCamera() and use that for EXTRA_OUTPUT. Hold onto that File object (and also save it in the saved instance state Bundle), then use that for finding out the size of the resulting video.

这篇关于在Android中使用URI获取文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆