Android 如何下载MP3文件
我想使用Android 如何下载MP3文件,android,multithreading,asynchronous,download,mp3,Android,Multithreading,Asynchronous,Download,Mp3,我想使用AsyncTask或线程下载mp3文件 我如何才能做到这一点?请参阅此线程: 你可以这样做 当您决定开始下载时: new Thread(new Runnable() { @Override public void run() { File out; Downloader DDL; DDL=new Downloader(); out=new File(Environment.getExternalStorag
AsyncTask
或线程下载mp3文件
我如何才能做到这一点?请参阅此线程:
你可以这样做 当您决定开始下载时:
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();
downloader类在哪里
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;
}
}
在中使用此功能可下载SD卡上的文件
public void downloadNauhe(String url) {
class DownloadFile extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... url) {
int count;
try {
URL url1 = new URL("http://downloads.hussainiat.com/nauhey/arsalan_haider/vol_2011_-_12/01_tum_jawab_e_zulm_dogay_-_arsalan_haider_2011_-_2012.mp3");
URLConnection conexion = url1.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
InputStream input = new BufferedInputStream(url1.openStream());
OutputStream output = new FileOutputStream("/sdcard/01_manum_abbas_as_-_mesum_abbas_2012.mp3");
byte data[] = new byte[1024];
long total = 0;
System.out.println("downloading.............");
while ((count = input.read(data)) != -1) {
total += count;
publishProgress((int)((total/(float)lenghtOfFile)*100));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
mprBar.setProgress(values[0]);
}
}
DownloadFile downloadFile = new DownloadFile();
downloadFile.execute(url);
}
public void downloadNauhe(字符串url){
类DownloadFile扩展异步任务{
@凌驾
受保护的字符串doInBackground(字符串…url){
整数计数;
试一试{
URL url1=新URL(“http://downloads.hussainiat.com/nauhey/arsalan_haider/vol_2011_-_12/01_tum_jawab_e_zulm_dogay_-_arsalan_haider_2011_-_2012.mp3");
URLConnection conexion=url1.openConnection();
conexion.connect();
int lenghtOfFile=conexion.getContentLength();
InputStream输入=新的BufferedInputStream(url1.openStream());
OutputStream output=新文件OutputStream(“/sdcard/01_manum_abbas_as_-_mesum_abbas_2012.mp3”);
字节数据[]=新字节[1024];
长总计=0;
System.out.println(“下载…………”);
而((计数=输入。读取(数据))!=-1){
总数+=计数;
出版进度((国际)(总/(浮动)长度)*100);
输出.写入(数据,0,计数);
}
output.flush();
output.close();
input.close();
}捕获(例外e){
}
返回null;
}
@凌驾
受保护的void onProgressUpdate(整型…值){
super.onProgressUpdate(值);
mprBar.setProgress(值[0]);
}
}
DownloadFile DownloadFile=新的DownloadFile();
downloadFile.execute(url);
}
别忘了添加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>