Получить размер файла видео из галереи в Android - PullRequest
0 голосов
/ 25 апреля 2019

У меня есть приложение, которое я хочу, чтобы пользователь загружал фильм и фотографию из галереи в приложении.Но программа останавливается для более чем 40 МБ фильмов.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 голосов
/ 25 апреля 2019
        File file = new File(filePath);
        long fileSizeInBytes = file.length();
        long fileSizeInKB = fileSizeInBytes / 1024;
        long fileSizeInMB = fileSizeInKB / 1024;
...