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

时间:2019-04-25 07:34:19

标签: android size android-videoview android-gallery

我有一个应用程序,希望用户从该应用程序的图库中加载电影和照片。但是该程序停止播放40 MB以上的电影。 1)如何获得所选影片的音量(大小)? 2)如何在不停止应用程序的情况下上传更大的电影

我已经尝试过了,但是返回了0

  long length = storeDirectory12.length();
  length = length/1024;
  Toast.makeText(getApplicationContext(), "Video size: "+length+" KB", Toast.LENGTH_LONG).show();

这是我的代码:

  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

        switch (requestCode) {
            case SELECT_PHOTO:
                if (resultCode == RESULT_OK) {
                    Uri selectedMedia = imageReturnedIntent.getData();
                    InputStream MediaStream = null;
                    try {
                        MediaStream = getContentResolver().openInputStream(selectedMedia);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    /////////////////////save in directory

                    String root = Environment.getExternalStorageDirectory().toString();
                    String name = "";

                    File myDir = new File(root + "/tahlilgar");
                    myDir.mkdirs();

                    if (!myDir.exists()) {
                        myDir.mkdir();
                    }


                            name = UUID.randomUUID().toString();
                            File storeDirectory12 = new File(root + "/tahlilgar/" + name + ".mp4");

                            InputStream inputStream = getContentResolver().openInputStream(selectedMedia);
                            FileOutputStream fileOutputStream = new FileOutputStream(storeDirectory12);
                            ByteArrayOutputStream bos = new ByteArrayOutputStream();
                            byte[] b = new byte[1024];

                            try {
                                for (int readNum; (readNum = inputStream.read(b)) != -1; ) {
                                    bos.write(b, 0, readNum);
                                }
                            } catch (Exception e) {

                            }
                            byte[] bytes = bos.toByteArray();
                            fileByte = bytes;
                            try {
                                fileOutputStream.write(bos.toByteArray());
                                fileOutputStream.flush();
                                fileOutputStream.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                            inputStream.close();
                    }

                    ////////////////////////convert file to byte
                    // fileww= convertVideoToBytes(selectedMedia);


                    // img.setImageBitmap(yourSelectedImage);
                }
        }

1 个答案:

答案 0 :(得分:0)

        File file = new File(filePath);
        long fileSizeInBytes = file.length();
        long fileSizeInKB = fileSizeInBytes / 1024;
        long fileSizeInMB = fileSizeInKB / 1024;