Java 应用程序看不到创建的文件
我创建了一个记录两个文件(来自麦克风和摄像机)的应用程序。录音工作正常,但现在我需要压缩音频文件并将压缩文件发送到服务器。 不幸的是,第二个创建的文件对应用程序不可见,即使在签入文件管理器时它位于文件夹中。我仍然得到“没有这样的文件或目录”错误。 我尝试使用以下行:Java 应用程序看不到创建的文件,java,file,sockets,tcp,zip,Java,File,Sockets,Tcp,Zip,我创建了一个记录两个文件(来自麦克风和摄像机)的应用程序。录音工作正常,但现在我需要压缩音频文件并将压缩文件发送到服务器。 不幸的是,第二个创建的文件对应用程序不可见,即使在签入文件管理器时它位于文件夹中。我仍然得到“没有这样的文件或目录”错误。 我尝试使用以下行: sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file))); 但这没有帮助 这是一个创建WAV文件的函数,我尝试使用se
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();
}
}
publicvoidzip(ArrayList\u文件,字符串zipFileName){
试一试{
zipPath=Environment.getExternalStorageDirectory().getAbsolutePath()+“/”+音频录制器文件夹+“/”+zipFileName;
BufferedInputStream原点=null;
FileOutputStream dest=新的FileOutputStream(zipPath);
ZipOutputStream out=新ZipOutputStream(新缓冲输出流(
目的地);
int缓冲区=1024;
字节[]数据=新字节[1024];
对于(int i=0;i<_files.size();i++){
Log.v(“压缩”,“添加:”+_files.get(i));
FileInputStream fi=新的FileInputStream(_files.get(i));
原点=新的BufferedInputStream(fi,缓冲区);
ZipEntry entry=newzipentry(_files.get(i).substring(_files.get(i).lastIndexOf(“/”+1));
外出、外出(进入);
整数计数;
while((count=origin.read(数据,0,缓冲区))!=-1){
输出。写入(数据,0,计数);
}
origin.close();
}
out.close();
}捕获(例外e){
e、 printStackTrace();
}
}
但它不起作用。bug在哪里?我建议您传入一个
ArrayList
而不是ArrayList
。文件
比字符串
具有更多的功能,包括方法.exists()
,它可以在开始发送路径时告诉您路径是否正确,然后再开始将其发送到各处并弄糊涂。文件路径
未使用。