【问题标题】:Application does not see created file应用程序看不到创建的文件
【发布时间】:2019-11-14 22:35:40
【问题描述】:

我创建了一个记录两个文件(来自麦克风和摄像机)的应用程序。录音工作正常,但现在我需要压缩音频文件并将压缩文件发送到服务器。 不幸的是,第二个创建的文件对应用程序不可见,即使它在文件管理器中检查时位于文件夹中。我仍然收到“没有这样的文件或目录”错误。 我尝试使用以下行:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));

但它没有帮助。

这是一个创建 WAV 文件的函数,这里我尝试使用 sendBroadcast:

private void copyWaveFile(int mic){

        FileInputStream in;
        FileOutputStream out;
        long totalAudioLen;
        long totalDataLen;
        long longSampleRate = RECORDER_SAMPLERATE;
        int channels = 2;
        long byteRate = RECORDER_BPP * RECORDER_SAMPLERATE * channels/8;

        byte[] data = new byte[bufferSize];

        try {
            String filepath = Environment.getExternalStorageDirectory().getAbsolutePath();
            File file = new File(getFilename(mic)); //getFilename returns filename for a given mic

            in = new FileInputStream(getTempFilename(mic));
            out = new FileOutputStream(file);

            totalAudioLen = in.getChannel().size();
            totalDataLen = totalAudioLen + 36;

            WriteWaveFileHeader(out, totalAudioLen, totalDataLen,
                    longSampleRate, channels, byteRate); //here I write header for a WAV file

            while(in.read(data) != -1) {
                out.write(data);
            }

            in.close();
            out.close();
            sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


这里是压缩功能:

public void zip(ArrayList<String> _files, String zipFileName) {
        try {
            zipPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + AUDIO_RECORDER_FOLDER + "/" + zipFileName;
            BufferedInputStream origin = null;
            FileOutputStream dest = new FileOutputStream(zipPath);
            ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
                    dest));
            int BUFFER = 1024;
            byte[] data = new byte[1024];

            for (int i = 0; i < _files.size(); i++) {
                Log.v("Compress", "Adding: " + _files.get(i));
                FileInputStream fi = new FileInputStream(_files.get(i));
                origin = new BufferedInputStream(fi, BUFFER);

                ZipEntry entry = new ZipEntry(_files.get(i).substring(_files.get(i).lastIndexOf("/") + 1));
                out.putNextEntry(entry);
                int count;

                while ((count = origin.read(data, 0, BUFFER)) != -1) {
                    out.write(data, 0, count);
                }
                origin.close();
            }

            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

但它不起作用。错误在哪里?

【问题讨论】:

  • filepath 未使用。

标签: java file sockets tcp zip


【解决方案1】:

我建议你传入ArrayList&lt;File&gt; 而不是ArrayList&lt;String&gt;FileString 有更多的功能,包括方法 .exists() 可以告诉你一开始你的路径是否正确,然后你开始到处发送它并让自己感到困惑。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-11
    • 2011-07-05
    • 2013-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-29
    • 1970-01-01
    相关资源
    最近更新 更多