0
A
回答
2
你可以做這樣的事情......
當你決定開始下載:
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();
凡下載類
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;
}
}
0
0
使用此函數用於下載文件在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);
}
Don'forget添加權限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
相關問題
- 1. 下載MP3文件
- 2. 如何播放下載的mp3文件?
- 3. Android的MP3文件下載
- 4. 可下載的MP3文件
- 5. 如何在流模式下阻止MP3文件被Flash下載
- 6. Android下載mp3文件並播放它
- 7. Nginx強制下載mp3文件
- 8. 無法播放下載的.mp3文件
- 9. javame - 下載並保存mp3文件
- 10. 下載時替換文件名mp3
- 11. 安卓下載和命名MP3文件
- 12. 如何使用Anchor標籤在Firefox中下載mp3文件?
- 13. 如何從Dictionary.com下載MP3文件在Matlab中?
- 14. 如何使用python從網站下載mp3文件
- 15. 如何使用Java在線下載mp3文件?
- 16. 在Ajax成功下載.mp3或.zip文件下載
- 17. 鏈接給力的MP3文件下載到默認的下載文件夾
- 18. Android Youtube下載mp3
- 19. 使鉻下載mp3
- 20. 在Android上下載java文件中的mp3文件
- 21. 如何從RAW文件夾,而不是SDCARD加載MP3文件
- 22. 如何混合Mp3文件
- 23. 如何獲得MP3文件
- 24. 如何下載RAR文件?
- 25. 如何下載此文件?
- 26. MP3文件的漸進式下載(流媒體和下載) - iPhone SDK
- 27. 多個MP3下載鏈接到一個zip文件下載JavaScript或jQuery的
- 28. 如何下載文件到我的下載文件夾android
- 29. 如何從文件夾下載文件?
- 30. 從url下載mp3,php