我正在尝试创建一个程序,将一个mp3保存到Android,可以通过ASTRO访问。我正在尝试将文件保存到/ sdcard / media / audio。尽管应用程序在Logcat / DDMS上没有出现任何错误,但该文件无处可寻。我添加了WRITE_EXTERNAL_STORAGE权限(以及Internet等),所以不是这样。
对于将文件保存到SD卡的应用有限制吗?
方法叫:
public void findSong(View view) throws Exception {
edittext = (EditText) findViewById(R.id.edittext);
searchTerm = edittext.getText().toString();
String address;
address = getMusic(searchTerm);
ImageManager img = new ImageManager(); //calls ImageManager (below)
img.DownloadFromUrl(address, title + ".mp3");
}
下载方法:
private final String PATH = "/data/data"; //put the downloaded file here
public void DownloadFromUrl(String imageURL, String fileName) {
//this is the downloader method
try {
URL url = new URL(imageURL); //you can write here any link
File file = new File(fileName);
long startTime = System.currentTimeMillis();
Log.d("ImageManager", "download begining");
Log.d("ImageManager", "download url:" + url);
Log.d("ImageManager", "downloaded file name:" + fileName);
/* Open a connection to that URL. */
URLConnection ucon = url.openConnection();
/* Define InputStreams to read from the URLConnection. */
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
/* Read bytes to the Buffer until there is nothing more to read(-1). */
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
/* Convert the Bytes read to a String. */
FileOutputStream fos = new FileOutputStream(PATH + file);
fos.write(baf.toByteArray());
fos.close();
Log.d("ImageManager", "download ready in"
+ ((System.currentTimeMillis() - startTime) / 1000)
+ " sec");
} catch (IOException e) {
Log.d("ImageManager", "Error: " + e);
}
}
任何帮助表示感谢。
答案 0 :(得分:2)
如果文件是mp3,你应该这样做:
Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));
这将扫描并在媒体播放器中显示。