1

So in my app I am loading images using AsyncImage

@Compose

...

AsyncImage(model = link,
    contentDescription = title,
    ...

And it loads the images in the app using jetpack compose. I've made a download button and when I click I want to download an image to the external storage with downloadManager.

...

val requestPermissionLauncher =
    rememberLauncherForActivityResult(ActivityResultContracts.RequestPermission()
    ){ isGranted: Boolean ->
        if(isGranted){
            Log.i("Permission: ", "Granted")
        }
        else {
            Log.i("Permission: ", "Denied")
        }

}

IconButton(
   onClick = {
       if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
       {
            if(ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) 
            {
                  // IF PERMISSION GRANTED DOWNLOAD IMAGE
                  fun downloading(link, title, context)
                  Toast.makeText(context, "Downloaded", Toast.LENGTH_SHORT).show()

            }

        
            else if(ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) 
            {
                     //PERMISSION HAS NOT BEEN ASKED YET
                     requestPermissionLauncher.launch(
                          Manifest.permission.WRITE_EXTERNAL_STORAGE
                     )

             }
        }
                            
        else{
              // IF <= Build.VERSION_CODES.Q, NO NEED FOR PERMISSION
              fun downloading(link, title, context)
              Toast.makeText(context, "Downloaded", Toast.LENGTH_SHORT).show()
        }
   }) {
         Icon(
              Icons.Outlined.ArrowDropDown,
              contentDescription = "Download Button",
          )}

This is the function to download the image. But it doesn't do anything when I click the button. I have the permissions on the AndroidManifest.xml. I looked at my downloaded images and they are not there. The permission works fine and I'm not getting any errors. I just can't download the image.


fun downloading(imageLink:String, title:String, context: Context){

        val request = DownloadManager.Request(Uri.parse(imageLink))
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE)
        request.setTitle("Download")
        request.setDescription("Downloading Image")
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
            "${System.currentTimeMillis()}")
    
        val manager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
        manager.enqueue(request)

}

AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    android:maxSdkVersion="28" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER" />
EGD
  • 11
  • 1

0 Answers0