我创建了一个应用程序,该应用程序记录了两个文件(来自麦克风和便携式摄像机)。录音工作正常,但现在我需要压缩音频文件并将其发送到服务器。 不幸的是,第二个创建的文件即使在检入文件管理器时位于文件夹中,也对应用程序不可见。我仍然收到“没有这样的文件或目录”错误。 我尝试使用以下行:
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();
}
}
但不起作用。错误在哪里?
答案 0 :(得分:0)
我建议您传递ArrayList<File>
而不是ArrayList<String>
。 File
比String
具有更多的功能,包括方法.exists()
,该方法可以告诉您在开始发送该路径之前,路径是否正确。并迷惑自己。