Skip to content

Instantly share code, notes, and snippets.

@siifii
Last active May 3, 2020 20:01
Show Gist options
  • Save siifii/042dd3d7fc119de437b08134510df76b to your computer and use it in GitHub Desktop.
Save siifii/042dd3d7fc119de437b08134510df76b to your computer and use it in GitHub Desktop.
Download File with download manager
var downloadID: Long = 0L
var manager: DownloadManager? = null
private val onDownloadComplete: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1)
if (downloadID == id) {
openDownloadedAttachment(context, downloadID)
}
}
}
// TODO PUT THIS IS YOUR onCreate
registerReceiver(onDownloadComplete, IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE))
private fun downloadBook(id: String, title: String) {
val request =
DownloadManager.Request(Uri.parse(BuildConfig.TIQAN_SERVER_URL + "files/$id"))
.addRequestHeader("Authorization", "YOUR_AUTH")
val tempTitle = title.replace(" ", "_")
request.setTitle(tempTitle)
// 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.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS,
"$tempTitle.pdf"
)
manager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
request.setMimeType("application/pdf")
request.allowScanningByMediaScanner()
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE or DownloadManager.Request.NETWORK_WIFI)
downloadID = manager!!.enqueue(request)
}
private fun openDownloadedAttachment(
context: Context,
downloadId: Long
) {
val downloadManager =
context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
val query = DownloadManager.Query()
query.setFilterById(downloadId)
val cursor = downloadManager.query(query)
if (cursor.moveToFirst()) {
val downloadStatus =
cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))
val downloadLocalUri =
cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))
val downloadMimeType =
cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_MEDIA_TYPE))
if (downloadStatus == DownloadManager.STATUS_SUCCESSFUL && downloadLocalUri != null) {
openDownloadedAttachment(
context,
Uri.parse(downloadLocalUri),
downloadMimeType
)
}
}
cursor.close()
}
private fun openDownloadedAttachment(
context: Context,
attachmentUri: Uri,
attachmentMimeType: String
) {
var attachmentUri: Uri? = attachmentUri
if (attachmentUri != null) {
// Get Content Uri.
if (ContentResolver.SCHEME_FILE == attachmentUri.scheme) {
// FileUri - Convert it to contentUri.
val file = File(attachmentUri.path)
attachmentUri =
FileProvider.getUriForFile(this, "YOUR_PACKAGENAME" + .provider", file)
}
val openAttachmentIntent = Intent(Intent.ACTION_VIEW)
openAttachmentIntent.setDataAndType(attachmentUri, attachmentMimeType)
openAttachmentIntent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
try {
requestOrderPb.gone()
context.startActivity(openAttachmentIntent)
} catch (e: ActivityNotFoundException) {
Toast.makeText(
context,
context.getString(R.string.unable_to_open_file),
Toast.LENGTH_LONG
).show()
}
}
}
override fun onDestroy() {
super.onDestroy()
unregisterReceiver(onDownloadComplete)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment