2010-12-07 116 views
2

我正在尝试创建一个程序,将MP3保存到Android,可以通过ASTRO访问。我试图将文件保存到/ sdcard/media/audio。尽管应用程序在Logcat/DDMS上没有发生任何错误,但无法找到该文件。我添加了WRITE_EXTERNAL_STORAGE权限(以及Internet等),所以不是这样。Android:将文件保存到SD卡?

是否有应用程序将文件保存到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); 
    } 
} 

任何帮助表示赞赏。

+1

尝试一下就像写出一个静态内容的虚拟文本文件,它不必在您的onCreate()中下载 - 通常不是一个好主意,但您只需要在代码中包含几个分钟来验证你已经想出了如何写文件。 – 2010-12-07 22:06:17

+1

您的代码显示您将文件放在'/ data/data'中,但是您的问题提及您将其放入'/ sdcard/media/audio' – Squonk 2010-12-07 23:52:49

回答

2

如果该文件是一个MP3,你应该做的:

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

这将拉福斯的扫描,并使其在媒体播放器使用。