Android mp3未正确保存到sd;如何将mp3保存到sd卡?
在过去的3个多小时里,我一直在浏览这个网站 这是我能想出的最好办法,因为我一次只复制一个文件Android mp3未正确保存到sd;如何将mp3保存到sd卡?,android,inputstream,outputstream,Android,Inputstream,Outputstream,在过去的3个多小时里,我一直在浏览这个网站 这是我能想出的最好办法,因为我一次只复制一个文件 InputStream in = null; OutputStream out = null; public void copyAssets() { try { in = getAssets().open("aabbccdd.mp3"); File outFile = new File(root.getAbsolutePath() + "/testf0lder
InputStream in = null;
OutputStream out = null;
public void copyAssets() {
try {
in = getAssets().open("aabbccdd.mp3");
File outFile = new File(root.getAbsolutePath() + "/testf0lder");
out = new FileOutputStream(outFile);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (IOException e) {
Log.e("tag", "Failed to copy asset file: ", e);
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
}
我已经知道如何创建文件和保存文本文件
我宁愿将mp3文件保存到SD卡,而不是文本文件
当我使用我提供的代码时,我会得到一个与aabbccdd.mp3文件大小相同的文本文档。它不会创建文件夹并保存.mp3文件。它将文本文档保存在根文件夹中。当你打开它时,我看到一大堆中文字母,但在英文的顶部我可以看到单词WireTap。WireTap Pro是我用来录制声音的程序,因此我知道.mp3正在播放。它只是不像上面的.edu示例那样创建一个文件夹然后保存一个文件
我该怎么办?我想你应该这样做-[注意:我用它来处理一些其他格式,不是mp3,但它在我的应用程序中适用于多种格式,所以我希望它也适用于你。]
InputStream in = this.getAssets().open("tmp.mp3"); //give path as per ur app
byte[] data = getByteData(in);
确保路径上已存在该文件夹,如果文件夹不存在,则无法正确保存内容
byteArrayToFile(data , "testfolder/tmp.mp3"); //as per ur sdcard path, modify it.
现在方法是:
1从inputstream获取的getByteData-
private byte[] getByteData(InputStream is)
{
byte[] buffer= new byte[1024]; /* or some other number */
int numRead;
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try{
while((numRead = is.read(buffer)) > 0) {
bytes.write(buffer, 0, numRead);
}
return bytes.toByteArray();
}
catch(Exception e)
{ e.printStackTrace(); }
return new byte[0];
}
2字节数组文件
public void byteArrayToFile(byte[] byteArray, String outFilePath){
FileOutputStream fos;
try {
fos = new FileOutputStream(outFilePath);
fos.write(byteArray);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
你确定这是文本文档吗?你用什么打开文件?windows记事本有一个众所周知的“错误”,导致它检测到一些文本文件为unicode,并将所有内容显示为中文。@losethequit这有帮助吗?还是你找到了其他的解决办法?