DownloadManager does not start file download
Imagine I want to download this file (random file): http://www.analysis.im/uploads/seminar/pdf-sample.pdf
This is my code:
DownloadManager.Request req = new DownloadManager.Request(Uri.parse("http://www.analysis.im/uploads/seminar/pdf-sample.pdf"));
req.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle("Random title")
.setDescription("Random description")
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "pdf-sample.pdf");
In debug mode, I see that all the options are correct, so why doesn't the download start?
EDIT
My current permissions:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
+3
source to share
3 answers
You are allowed to download in network type DownloadManager.Request.NETWORK_MOBILE
, but why did you install setAllowedOverRoaming(false)
?
I tried to use Downloadmanager
to upload a file, here is my code:
String url = "http://example.com/large.zip";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
// only download via WIFI
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
request.setTitle("Example");
request.setDescription("Downloading a very large zip");
// we just want to download silently
request.setVisibleInDownloadsUi(false);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
request.setDestinationInExternalFilesDir(context, null, "large.zip");
// enqueue this request
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
downloadID = downloadManager.enqueue(request);
Hope you are inspired.
+2
source to share
Follow the steps below to download a random file. Create a class called DownloadFileAsync.java
public class DownloadFileAsync extends AsyncTask<String, String, String> {
public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private ProgressDialog mProgressDialog;
private Context context;
public DownloadFileAsync(Context context)
{
this.context = context;
mProgressDialog = new ProgressDialog(context);
mProgressDialog.setMessage("Downloading file..");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(true);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog.show();
}
@Override
protected String doInBackground(String... aurl) {
try {
File root = Environment.getExternalStorageDirectory();
URL u = new URL(aurl[0]);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
int lenghtOfFile = c.getContentLength();
FileOutputStream f = new FileOutputStream(new File(root + "/", aurl[1]));
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
long total = 0;
while ((len1 = in.read(buffer)) > 0) {
total += len1; //total = total + len1
publishProgress("" + (int)((total*100)/lenghtOfFile));
f.write(buffer, 0, len1);
}
f.close();
} catch (Exception e) {
Log.d("Downloader", e.getMessage());
}
return null;
}
protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC",progress[0]);
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String unused) {
mProgressDialog.dismiss();
}
}
Call the above class in your activity as
new DownloadFileAsync(TransformerActivity.this).execute(Constants.VIDEO_DOWNLOAD_LINK,"9T51B0108.mp4");
0
source to share