如何下载MP3文件 [英] How to download an MP3 file

查看:124
本文介绍了如何下载MP3文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用下载一个MP3文件中的的AsyncTask 或线程。

I want to download an mp3 file using an AsyncTask or a thread.

我怎样才能做到这一点?

How can I do this?

推荐答案

您可以做这样的事情...

You can do something like this...

当你决定开始下载:

new Thread(new Runnable()
  {
  @Override
  public void run()
       {
       File out;
       Downloader DDL;
       DDL=new Downloader();
       out=new File(Environment.getExternalStorageDirectory() + "/DestFileName.txt");
       DDL.DownloadFile("SourceURL",out);

       }
    }).start();

在哪里下载器类

Where the downloader class is

public class Downloader {

public void Downloader() 
        {
    // TODO Auto-generated method stub
    }

public boolean DownloadFile(String url, File outputFile) 
    {
    try {
      URL u = new URL(url);
      URLConnection conn = u.openConnection();
      int contentLength = conn.getContentLength();

      DataInputStream stream = new DataInputStream(u.openStream());

      byte[] buffer = new byte[contentLength];
      stream.readFully(buffer);
      stream.close();

      DataOutputStream fos = new DataOutputStream(new FileOutputStream(outputFile));
      fos.write(buffer);
      fos.flush();
      fos.close();
      } 
    catch(FileNotFoundException e) 
      {
      return false; 
      } 
    catch (IOException e) 
      {
      return false; 
      }

    return true;
    }
}

这篇关于如何下载MP3文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆