簡體   English   中英

從android中的gallery獲取視頻的文件大小

[英]Get file size of video from gallery in android

我有一個應用程序,我希望用戶從應用程序中的庫中加載電影和照片。 但該節目停止播放超過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);
                }
        }
        File file = new File(filePath);
        long fileSizeInBytes = file.length();
        long fileSizeInKB = fileSizeInBytes / 1024;
        long fileSizeInMB = fileSizeInKB / 1024;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM