应用程序看不到已创建的文件



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

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();
}
}

但它不起作用。虫子在哪里?

我建议您传入ArrayList<File>而不是ArrayList<String>FileString有更多的功能,包括.exists()方法,它可以在你开始到处发送并混淆自己之前,告诉你一开始路径是否正确。

最新更新