2

我正在尝试创建一个将 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);
   }
}

任何帮助表示赞赏。

4

1 回答 1

2

如果文件是 mp3,你应该这样做:

Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));

这将进行扫描并使其在媒体播放器中可用。

于 2010-12-07T21:55:36.943 回答