سلام بفرما اینم برای شما فقط قسمت های لینک دانلود و ادرس محل ذخیره رو خودت تغییر بده تو مین فیست هم یادت نره دسترسی های لازم رو داشته باشی مثل ینترنت و نوشتن روی کارت sd
private void startDownload() {
String url = "http://upir.ir/files92be/724dc757544d.mp3";
new DownloadFileAsync().execute(url);
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DOWNLOAD_PROGRESS:
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Downloading file..");
mProgressDialog.setProgressStyle(ProgressDialog.ST YLE_HORIZONTAL);
mProgressDialog.setCancelable(true);
mProgressDialog.show();
mProgressDialog.closeOptionsMenu();
mProgressDialog.setCanceledOnTouchOutside(false);
return mProgressDialog;
default:
return onCreateDialog(RESULT_OK);
}
}
@SuppressLint("ParserError")
class DownloadFileAsync extends AsyncTask<String, String, String> {
@SuppressWarnings("deprecation")
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_PROGRESS);
}
@Override
protected String doInBackground(String... aurl) {
int count;
try {
URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/mnt/sdcard/englishforyou/m1.mp3");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
OnFinished();
} catch (Exception e) {}
return null;
}
protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC",progress[0]);
mProgressDialog.setProgress(Integer.parseInt(progr ess[0]));
}
}