【发布时间】:2012-06-08 14:29:45
【问题描述】:
我使用
从服务器下载音频文件 try {
// URL url = new URL("http://commonsware.com/misc/test2.3gp");
URL url = new URL("http://192.168.0.2/supplications/"+fileName);
//URL url = new URL("http://www.msoftech.com/supplications/android/"+fileName);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
Log.v("log_tag", "PATH: " + PATH);
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, fileName);
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1)
{
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
} catch (IOException e) {
Log.d("log_tag", "Error: " + e);
}
Log.v("log_tag", "Check: " +cd2);
这里 PATH = "/data/data/packagename/sounds/filename
它工作正常,音频文件下载并播放成功,但我的问题是当我点击主页按钮然后重新启动应用程序时意味着没有找到下载音频的文件夹,即退出应用程序时意味着所有下载音频被自动删除。它抛出未找到的异常文件。
为了播放下载的文件,我使用了如下代码,
public void audioPlayer(String path, String fileName) throw FileNotFoundException
{
//set up MediaPlayer
FileInputStream fileInputStream = new FileInputStream(PATH+"/"+fileName);
//String command = "chmod 666 " + recordFile.toString();
try {
mp.setDataSource(fileInputStream.getFD());
// mp.setDataSource(path+"/"+filename.mp3);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
有什么问题我必须做些什么才能永久保存音频文件。
【问题讨论】:
-
文件保存在哪里?如果是缓存或内部存储,它可能会被 Android 删除。
-
我把文件保存在路径,"/data/data/com.do/sounds/filename.mp3
-
xandy 是正确的。此外android建议将媒体文件存储到SD卡。
-
将日志放入公共 void audioPlayer(String path, String fileName) 并查看您正在获取的路径。我担心您正在从错误的路径读取..或者以某种方式将路径初始化为 null
-
内部内存只允许大约 1 或 2 Mb(不确定),很可能不足以容纳您的 mp3 文件。
标签: java android audio download filenotfoundexception