Updated 22 April 2017
There are many methods to download files from the server.
I am discussing with DownLoadManager, it is applied on the GingerBread or above version of applications.
GingerBread brought a new feature, DownloadManager,
which allows you to download files easily and delegate the hard work of handling threads, streams, etc. to the system.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
public void downloadPdfFile(final String title){ AlertDialog.Builder alert = new AlertDialog.Builder(context); alert.setNegativeButton(context.getResources().getString(android.R.string.cancel), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); ((Checkout)context).pdfCreater = null; } }); alert.setPositiveButton(context.getResources().getString(android.R.string.ok), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { try { DownloadManager.Request request = new DownloadManager.Request(Uri.parse(Html.fromHtml(mUrl) + "")); request.setTitle(title); // in order for this if to run, you must use the android 3.2 to compile your app if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { request.allowScanningByMediaScanner(); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); } // request.setDestinationInExternalFilesDir(context,Environment.DIRECTORY_DOWNLOADS, "ALSupermarket"); request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName); // // get download service and enqueue file manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); long index = manager.enqueue(request); context.registerReceiver(receiver_complete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); // manager.openDownloadedFile(index); // uri = manager.getUriForDownloadedFile(index); // Log.d("downloading url = ", uri.toString()); } catch (Exception e) { e.printStackTrace(); } if(title.equalsIgnoreCase("ALSupermarket")) Toast.makeText(context, R.string.download_started_popup, Toast.LENGTH_SHORT).show(); else { Toast.makeText(context, R.string.shareing_start_popup, Toast.LENGTH_SHORT).show(); } } }); alert.setCancelable(false); alert.setMessage(Html.fromHtml(context.getString(R.string.ask_for_download))).show(); } |
Find alert message when File has downloaded-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
BroadcastReceiver receiver_complete = new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)){ Bundle extras = intent.getExtras(); DownloadManager.Query q = new DownloadManager.Query(); q.setFilterById(extras.getLong(DownloadManager.EXTRA_DOWNLOAD_ID)); Cursor c = manager.query(q); if (c.moveToFirst()) { int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)); if (status == DownloadManager.STATUS_SUCCESSFUL) { // process download if(c.getString(c.getColumnIndex(DownloadManager.COLUMN_TITLE)).equalsIgnoreCase("View")) viewPdf(); else if(c.getString(c.getColumnIndex(DownloadManager.COLUMN_TITLE)).equalsIgnoreCase("Share")) shareFile(); // get other required data by changing the constant passed to getColumnIndex }else{ Toast.makeText(context, R.string.error_creating_pdf_file, Toast.LENGTH_SHORT).show(); } } } } }; |
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.