2010-12-07 136 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))); 

這將拉福斯的掃描,並使其在媒體播放器使用。