Downloading files in a queue using download manager in android? - android

I am downloading multiple file same time with download manager. but my problem is download manager start download all files with same time i need to start download file one by one in queue means first item download first than all one by one.
DownloadManager.Java
if(downloaddata.size()>0){
for(int i=0;i<downloaddata.size();i++){
downloadFiles(downloaddata.get(i).getFile_id(),downloaddata.get(i).getFile_url(),"OTHER");
}
}
public void downloadFiles(String myid,String myurl,String type){
createFolder();
String fileName = URLUtil.guessFileName(myurl, null, null);
Uri uri=Uri.parse(myurl);
request=new DownloadManager.Request(uri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
request.setTitle(myid);
request.setMimeType(type);
request.setDestinationInExternalPublicDir("/peakmedia",fileName); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
downloadReference=downloadManager.enqueue(request);
downloadlist.add(downloadReference);
}

DownloadManager broadcasts ACTION_DOWNLOAD_COMPLETE, so you can just request the next file to download when the BroadcastReceiver receives success for previous file.

Related

Clear cache of Download Manager programmatically in Android

I have an Android app that has a feature of downloading files. After using the app for some time, downloading files not working.
I came to know after some research, after a clear cache of download manager of the system, downloading files work completely without any issue.
That means this solution works perfectly. But that doesn't mean application users have to do that to use the app. So I want to do that using code.
I need any solution that can clear the cache of Download Manager of the system using the code.
Here it is template code for downloading:
IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
context.registerReceiver(downloadReceiver, filter);
downloadManager = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
Uri Download_Uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
//Restrict the types of networks over which this download may proceed.
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
//Set whether this download may proceed over a roaming connection.
request.setAllowedOverRoaming(false);
//Set the title of this download, to be displayed in notifications (if enabled).
request.setTitle(savedFileName);
//Set a description of this download, to be displayed in notifications (if enabled)
//equest.setDescription("Android Data download using DownloadManager.");
//Set the local destination for the downloaded file to a path within the application's external files directory
request.setDestinationInExternalPublicDir(download_path, savedFileName);
//Set visible and shows in the notifications while in progress and after completion.
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
//Enqueue a new download and same the referenceId
downloadReference = downloadManager.enqueue(request);
// receiver
private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(downloadReference);
Cursor c = downloadManager.query(query);
if (c.moveToFirst()) {
checkStatus(c);
}
};

Allow other apps to start my activity when the user touches a download link

There is a feature in ADM (Download Manager) that if the user touches a download link (not a web page), the ADM(Download Manager) will be appeared as an application that has the ability to download files.
What should I do that if the user touched a download link, my application will be appeared a an application that has the ability to download files?
DownloadData class
private long DownloadData (Uri uri, View v) {
long downloadReference;
// Create request for android download manager
downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(uri);
//Setting title of request
request.setTitle("Data Download");
//Setting description of request
request.setDescription("Android Data download using DownloadManager.");
//Set the local destination for the downloaded file to a path
//within the application's external files directory
if(v.getId() == R.id.DownloadMusic)
request.setDestinationInExternalFilesDir(MainActivity.this,
Environment.DIRECTORY_DOWNLOADS,"AndroidTutorialPoint.mp3");
else if(v.getId() == R.id.DownloadImage)
request.setDestinationInExternalFilesDir(MainActivity.this,
Environment.DIRECTORY_DOWNLOADS,"AndroidTutorialPoint.jpg");
//Enqueue download and save into referenceId
downloadReference = downloadManager.enqueue(request);
Button DownloadStatus = (Button) findViewById(R.id.DownloadStatus);
DownloadStatus.setEnabled(true);
Button CancelDownload = (Button) findViewById(R.id.CancelDownload);
CancelDownload.setEnabled(true);
return downloadReference;
}
Description of the above code:
downloadReference: It is a unique id that we will refer for specific download request.
request: Instance of DownloadManager will be created through getSystemService by passing
DOWNLOAD_SERVICE. A new request is generated in the next statement using DownloadManager.Request(uri).
setDestinationInExternalFilesDir: This will be used to save file in external downloads folder.
downloadManager.enqueue(request): Enqueue a new download corresponding to request. The download will start automatically once the download manager is ready to execute it and connectivity is available.
Source :https://www.codeproject.com/Articles/1112730/Android-Download-Manager-Tutorial-How-to-Download

Download Manager in Android

I'm developing a magazine app and I need to download one edition of the magazine to be available in offline mode. One edition may have 20 articles.
I'm using download manager to download the articles, enqueueing all of them, but I need a callback when finished to download all articles of one edition.
If I click to download two magazines at once, it'll enqueue all the articles of both magazines and I need that they be individual.
So far I have this:
mDownloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
for (final Materia materia : materiasList) {
String url = materia.url;
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("Baixando...");
request.setTitle(mEditionTitle + " - " + materia.titulo);
request.allowScanningByMediaScanner();
request.setDestinationInExternalFilesDir(mContext, "/editions/" + String.valueOf(mEditionId) + File.separator, String.valueOf(materia.id) + ".html");
// get download service and enqueue file
mDownloadManager.enqueue(request);
}
EDIT:
I'm running this manager from a service.
Not sure this is the best solution but you could query your download manager at regular interval and get all the in progress downloads. Then you parse the result and check if it is still downloading some items for a collection or if they are all downloaded.
Query the manager:
ArrayList<QueuedDownload> downloads = new ArrayList<>();
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Query myDownloadQuery = new DownloadManager.Query();
myDownloadQuery.setFilterByStatus(DownloadManager.STATUS_RUNNING | DownloadManager.STATUS_PAUSED | DownloadManager.STATUS_PENDING);
Cursor cursor = downloadManager.query(myDownloadQuery);
while (cursor.moveToNext()) {
// Parse each download
}
cursor.close();
Of course that should run on a background thread.

How I can download video in my application like we download or install apk file on google play?

I want to download video from URL in background like we install apk file in background from Google play. I want to show downloading indicator on notification bar same like it showing while we download or install apk file. if any suggestion or idea then suggest me how I can achieve this task in my project.
You can prefer android DownLoadManager. somehow want to do like this in your activity
String mUrl="your video url";
DownloadManager manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Request request = new Request(Uri.parse(mUrl));
request.setDestinationInExternalFilesDir(getApplicationContext(), Environment.DIRECTORY_DOWNLOADS, your_fileName);
request.setNotificationVisibility(Request.VISIBILITY_VISIBLE);
request.setAllowedOverRoaming(false);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
manager.enqueue(request);
setNotificationVisibility() -> show download notification in notification bar(window) with download progress also.
below are three methods where you can choose destination for your downloaded file path.
1. setDestinationInExternalFilesDir(Context context, String dirType, String subPath)
Set the local destination for the downloaded file to a path within the application's external files directory (as returned by getExternalFilesDir(String).
2. setDestinationInExternalPublicDir(String dirType, String subPath)
Set the local destination for the downloaded file to a path within the public external storage directory (as returned by getExternalStoragePublicDirectory(String)).
3. setDestinationUri(Uri uri)
Set the local destination for the downloaded file.
Use Download Manager,
private void for_call_download() {
File folder = Environment.getExternalStoragePublicDirectory(DOWNLOAD_FOLDER_NAME);
if (!folder.exists() || !folder.isDirectory()) {
folder.mkdirs();
}
try {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(APK_URL));
request.setDestinationInExternalPublicDir(DOWNLOAD_FOLDER_NAME, DOWNLOAD_FILE_NAME);
request.setTitle(SessionName);
request.setDescription("" + SessionDesc);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setVisibleInDownloadsUi(false);
request.setMimeType("application/cn.trinea.download.file");
downloadId = downloadManager.enqueue(request);
} catch (IllegalStateException i) {
showAlert(context, "Sorry Some Problem");
i.printStackTrace();
}
updateView();
status_download = true;
}

How to save downloaded file on sd card from download manager programmatically on android

In my application i have download(Image) feature which is used to download file from urls. The download happen should be shown in notification bar so that i used Download Manager class to download file. This is working fine but the downloaded image does not stored no where in the sdcard.
i have referred the url for the download manager.
my requirement is i need to save the download image to sdcard with notification bar indication. What to modify on the code to get save image on sdcard on the above link
i have some doubts regards the code in the above link is Can i use the same code to download audio or video file?
please help me.
Edited question:
I have tried
filepath = Environment.getExternalStorageDirectory().getPath()+"/download/cm.png";
Uri destinationUri = Uri.parse(filepath);
request.setDestinationUri(destinationUri);
before the preference manger on the button click. but i could not get the file on sdcard.
This is what i used.
Uri downloadUri = Uri.parse(DOWNLOAD_FILE);
DownloadManager.Request request = new DownloadManager.Request(downloadUri);
request.setDescription("Downloading a file");
long id = downloadManager.enqueue(request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle("File Downloading...")
.setDescription("Image File Download")
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "cm.png"));
In the code you refer to, the file is opened at the end. At this point, you can consider copying it to the SDCard.
Otherwise (better) use http://developer.android.com/reference/android/app/DownloadManager.Request.html setDestinationUri(android.net.Uri) to specify where you want to download the file.
downloadmanager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
Toast.makeText(context, "Downloading...", Toast.LENGTH_LONG).show();
Uri uri = Uri.parse("---- url here ------");
request = new DownloadManager.Request(uri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setAllowedOverRoaming(false);
request.setTitle("---- title here ------");
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl("---- url here ------");
request.setMimeType(mimeType);
request.setDescription("---- descripation here ------");
if("---- titlehere ------" != null){
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "---- title here ------");
}
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
Long reference = downloadmanager.enqueue(request);

Categories

Resources