Android 视频保存为所需的 1000 倍 space

Android video saving with 1000x the required space

我编写此代码是为了在我的应用程序中节省制作视频的时间,但出于某种我不明白的原因,它节省了比需要多 1000 倍的 space 使用量。

我知道这一点,因为保存在相机文件夹中的同一视频的使用量减少了 1000 倍space。

有什么想法吗?代码如下。

    public void trabalhaVideo(Intent data) {
        String state = Environment.getExternalStorageState();

        if (Environment.MEDIA_MOUNTED.equals(state)
            && (!Environment.isExternalStorageEmulated())) {

        Uri vid = data.getData();
        String videoPath = getRealPathFromURI(vid);
        File file = new File(videoPath);

        try {
            File local = new File(Environment.getExternalStorageDirectory()
                    + "/MEDGRUPO/videos/");

            FileInputStream fileInput = new FileInputStream(videoPath);

            if (!local.exists()) {
                local.mkdirs();
                File novo = new File(local, cal.getTimeInMillis() + ".mp4");
                FileOutputStream outStream = new FileOutputStream(novo);
                byte[] bFile = new byte[(int) file.length()];
                fileInput.read(bFile);
                fileInput.close();
                for (int i = 0; i < bFile.length; i++) {
                    outStream.write(bFile);
                }
                outStream.close();

            } else {
                File novo = new File(local, cal.getTimeInMillis() + ".mp4");
                FileOutputStream outStream = new FileOutputStream(novo);

                byte[] bFile = new byte[(int) file.length()];
                fileInput.read(bFile);
                fileInput.close();
                for (int i = 0; i < bFile.length; i++) {
                    outStream.write(bFile);
                }
                outStream.close();
            }
            CustomAdapter.pintaLinha(ViewDeAtividades.getChildAt(2));
            AtividadesDAO dao = new AtividadesDAO(getApplicationContext());
            dao.atualizaAtividadeFeita(atividadeSelecionada);
            Toast.makeText(getApplicationContext(),
                    "Vídeo gravado com sucesso", Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            e.fillInStackTrace();

        }

你的循环:

           for (int i = 0; i < bFile.length; i++) {
                outStream.write(bFile);
            }

最终将整个 bFile 字节数组写入 bFile.length 次。实际上你正在写 (bFile.length)2 字节。有一个outStream.write(bFile)就可以了,不用循环。如果你坚持把它放在一个循环中,那么你的循环应该看起来像

           for (int i = 0; i < bFile.length; i++) {
                outStream.write(bFile[i]);
            }