开发者

need help for android mp3 file download

i am developing an android application into it i need to download mp3 file

but the download process should be into the background.

does any one have any ide开发者_如何学Ca about this?


Finally I've got the solution.

public void mp3load() {
        URL url = new URL(url);
        HttpURLConnection c = (HttpURLConnection) url.openConnection();
        c.setRequestMethod("GET");
        c.setDoOutput(true);
        c.connect();

        String PATH = Environment.getExternalStorageDirectory()
                + "/download/";
        Log.v(LOG_TAG, "PATH: " + PATH);
        File file = new File(PATH);
        file.mkdirs();

         String fileName = "test.mp3";


        File outputFile = new File(file, fileName);
        FileOutputStream fos = new FileOutputStream(outputFile);

        InputStream is = c.getInputStream();

        byte[] buffer = new byte[1024];
        int len1 = 0;
        while ((len1 = is.read(buffer)) != -1) {
            fos.write(buffer, 0, len1);
        }
        fos.close();
        is.close();

}

It works good for me.


android: Is it possible to dowload file to disk given an xml (from a URL)?

Check this out: a AsyncTask class with a downloadFileFrom method will help you out.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜