Reputation: 476
I'm trying to make a download file prosess in my android apps with DownloadManager
class and put some Toast
with the download status. When I click download button, the Toast always say that my download is pending. Why it happend?
In DownloadManager
documnet I'd read that pending mean the download is waiting to start. What is waiting for? Where should I looking for? How do I change the status and start the download?
Here's my code :
public void downloadFile(){
Uri uri = Uri.parse("https://kopi81.000webhostapp.com/cccc.jpg");
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS,
"cccc.jpg");
Long ref = downloadManager.enqueue(request);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(ref);
Cursor cursor = downloadManager.query(query);
if(cursor.moveToFirst()){
DownloadStatus(cursor, ref);
}
}
I think my internet is also good no problem with it. The file that I'm trying to get is from my server on 000webhost.com. When I try to get any file from another server the download's start. Is my server that got the problem or my android application?
Upvotes: 0
Views: 1813
Reputation: 406
I wrote this code for you. I hope to help you ❤ :
private DownloadManager downloadManager;
private Uri Download_Uri;
private long refid;
public void downloadFile(){
registerReceiver(onComplete,
new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Download_Uri = Uri.parse("https://kopi81.000webhostapp.com/cccc.jpg");
DownloadManager.Request req = new DownloadManager.Request(Download_Uri);
req.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
req.setAllowedOverRoaming(false);
req.setTitle("Downloading ... " );
req.setDescription("Downloading " + "cccc.jpg");
req.setVisibleInDownloadsUi(true);
req.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "/Kopi81/" + "cccc.jpg");
refid = downloadManager.enqueue(req);
}
BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"Download completed !",Toast.LENGTH_SHORT).show();
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Kopi81")
.setContentText("Download completed !");
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(455, mBuilder.build());
}
};}
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
I tested on my device [Sony Z3] & [Sony XZ] and had no problem !
good luck.
Upvotes: 2