在Android中的SD卡中从服务器下载MP3文件

时间:2015-10-10 07:58:09

标签: android

我想逐个从服务器下载所有mp3文件并保存到SD卡文件夹中。我没有任何错误或异常,但mp3没有下载,也没有显示在SD卡中。有人可以帮助解决这个问题。这是我的代码。

if (imageName.endsWith(mp3_Pattern))
                    {
                        str_DownLoadUrl = namespace + "/DownloadFile/FileName/" + imageName;

                        Log.e("#########  ", "str_DownLoadUrl  = " + str_DownLoadUrl);
                        download_Mp3File(str_DownLoadUrl);

                        strDownLoadStatus = "1";
                        dbhelper.update_DownLoadStatus(imageName, strDownLoadStatus);
                    }

 void download_Mp3File(final String fileUrl) {

        new AsyncTask<String, Integer, String>()
        {
            @Override
            protected String doInBackground(String... arg0)
            {
                int count;
                File file = new File(newFolder, System.currentTimeMillis() + imageName);
                try
                {
                    URL url = new URL(fileUrl);
                    URLConnection conexion = url.openConnection();
                    conexion.connect();
                    // this will be useful so that you can show a tipical 0-100% progress bar
                    int lenghtOfFile = conexion.getContentLength();

                    // downlod the file
                    InputStream input = new BufferedInputStream(url.openStream());
                    OutputStream output = new FileOutputStream(file);

                    byte data[] = new byte[1024];

                    long total = 0;

                    while ((count = input.read(data)) != -1) {
                        total += count;
                        // publishing the progress....
                        publishProgress((int) (total * 100 / lenghtOfFile));
                        output.write(data, 0, count);
                    }

                    output.flush();
                    output.close();
                    input.close();
                } catch (Exception e) {
                }
                return  null;
            }
        }.execute();

    }

1 个答案:

答案 0 :(得分:0)

在inputstream对象中放置一个断点,看是否有任何流。然后调试输出流以查看结果。