Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MyImageDownloader {
- private var msg: String? = ""
- private var lastMsg = ""
- private var imageUrl = ""
- private lateinit var context: Activity
- fun getImage(activity: Activity, url: String) {
- imageUrl = url
- context = activity
- // After API 23 (Marshmallow) and lower Android 10 you need to ask for permission first before save an image
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
- askPermissions(url)
- } else {
- downloadImage(url)
- }
- }
- @TargetApi(Build.VERSION_CODES.M)
- fun askPermissions(imageUrl: String) {
- if (ContextCompat.checkSelfPermission(
- context,
- Manifest.permission.WRITE_EXTERNAL_STORAGE
- ) != PackageManager.PERMISSION_GRANTED
- ) {
- // Permission is not granted
- // Should we show an explanation?
- if (ActivityCompat.shouldShowRequestPermissionRationale(
- context,
- Manifest.permission.WRITE_EXTERNAL_STORAGE
- )
- ) {
- // Show an explanation to the user *asynchronously* -- don't block
- // this thread waiting for the user's response! After the user
- // sees the explanation, try again to request the permission.
- AlertDialog.Builder(context)
- .setTitle("Permission required")
- .setMessage("Permission required to save photos from the Web.")
- .setPositiveButton("Allow") { dialog, id ->
- ActivityCompat.requestPermissions(
- context,
- arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
- MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE
- )
- context.finish()
- }
- .setNegativeButton("Deny") { dialog, id -> dialog.cancel() }
- .show()
- } else {
- // No explanation needed, we can request the permission.
- ActivityCompat.requestPermissions(
- context,
- arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
- MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE
- )
- // MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE is an
- // app-defined int constant. The callback method gets the
- // result of the request.
- }
- } else {
- // Permission has already been granted
- downloadImage(imageUrl)
- }
- }
- fun onRequestPermissionsResult(
- requestCode: Int,
- permissions: Array<String>,
- grantResults: IntArray
- ) {
- when (requestCode) {
- MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE -> {
- // If request is cancelled, the result arrays are empty.
- if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
- // permission was granted, yay!
- // Download the Image
- downloadImage(imageUrl)
- } else {
- // permission denied, boo! Disable the
- // functionality that depends on this permission.
- }
- return
- }
- // Add other 'when' lines to check for other
- // permissions this app might request.
- else -> {
- // Ignore all other requests.
- }
- }
- }
- private fun downloadImage(url: String) {
- val directory = File(Environment.DIRECTORY_PICTURES)
- if (!directory.exists()) {
- directory.mkdirs()
- }
- val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
- val downloadUri = Uri.parse(url)
- val request = DownloadManager.Request(downloadUri).apply {
- setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE)
- .setAllowedOverRoaming(false)
- .setTitle(url.substring(url.lastIndexOf("/") + 1))
- .setDescription("")
- .setDestinationInExternalPublicDir(
- directory.toString(),
- url.substring(url.lastIndexOf("/") + 1)
- )
- }
- val downloadId = downloadManager.enqueue(request)
- val query = DownloadManager.Query().setFilterById(downloadId)
- Thread(Runnable {
- var downloading = true
- while (downloading) {
- val cursor: Cursor = downloadManager.query(query)
- cursor.moveToFirst()
- if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
- downloading = false
- }
- val status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))
- msg = statusMessage(url, directory, status)
- if (msg != lastMsg) {
- context.runOnUiThread {
- Toast.makeText(context, msg, Toast.LENGTH_SHORT).show()
- }
- lastMsg = msg ?: ""
- }
- cursor.close()
- }
- }).start()
- }
- private fun statusMessage(url: String, directory: File, status: Int): String? {
- var msg = ""
- msg = when (status) {
- DownloadManager.STATUS_FAILED -> "Download has been failed, please try again"
- DownloadManager.STATUS_PAUSED -> "Paused"
- DownloadManager.STATUS_PENDING -> "Pending"
- DownloadManager.STATUS_RUNNING -> "Downloading..."
- DownloadManager.STATUS_SUCCESSFUL -> "Image downloaded successfully in $directory" + File.separator + url.substring(
- url.lastIndexOf("/") + 1
- )
- else -> "There's nothing to download"
- }
- return msg
- }
- companion object {
- private const val MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 1
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement