title: Download video from url in kotlin Android?
body: <p>I want to download a video from URL. but it give an error:</p>
  
  <blockquote>
    <p>E/ContentProviderNative: onTransact error from {P:12600;U:11003}
    2019-01-14 11:54:35.423 3518-22398/? E/DatabaseUtils: Writing exception to parcel
        java.lang.IllegalArgumentException: Unknown URI: content://downloads/public_downloads/6796
            at com.android.providers.downloads.DownloadProvider.query(DownloadProvider.java:914)
            at android.content.ContentProvider.query(ContentProvider.java:1138)
            at android.content.ContentProvider.query(ContentProvider.java:1230)
            at android.content.ContentProvider$Transport.query(ContentProvider.java:251)
            at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112)
            at android.os.Binder.execTransact(Binder.java:752)
    <a href="https://i.stack.imgur.com/Sbv1w.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Sbv1w.png" alt="enter image description here"></a></p>
  </blockquote>
  
  <p>code for downloding a video file is below:
      private var downloadReference: Long = 0
      private lateinit var downloadManager: DownloadManager</p>
  
  <pre><code>private val receiver = object : BroadcastReceiver() {
      override fun onReceive(context: Context, intent: Intent) {
          val action = intent.action
          if (action == DownloadManager.ACTION_DOWNLOAD_COMPLETE) {
              val downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1)
              if (downloadId != downloadReference) {
                  context.unregisterReceiver(this)
                  return
              }
              val query = DownloadManager.Query()
              query.setFilterById(downloadReference)
              val cursor = downloadManager.query(query)
              cursor?.let {
                  if (cursor.moveToFirst()) {
                      val columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)
                      if (DownloadManager.STATUS_SUCCESSFUL == cursor.getInt(columnIndex)) {
                          var localFile = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))
  
                          if (localFile.contains("file:///")) {
                              localFile = localFile.removePrefix("file:///").substringBeforeLast(File.separator)
                          }
                          //context.toast(context.resources.getString(R.string.saved, localFile), Toast.LENGTH_LONG)
  
                      } else if (DownloadManager.STATUS_FAILED == cursor.getInt(columnIndex)) {
                        //  val message = context.resources.getString("error : ", cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_REASON)))
                          //context.toast(message, Toast.LENGTH_LONG)
                      }
                  }
                  cursor.close()
              }
  
              context.unregisterReceiver(this)
  
          }
      }
  }
  
  /////////// New Testing........
  fun downloadFile(url: String, mimeType: String? = null) {
  
      val guessFileName = URLUtil.guessFileName(url, null, mimeType)
      System.out.println("LLLLLLLLLLLLLLLLLLL2 ");
     // Timber.d("mimeType -&gt; $mimeType guessFileName -&gt; $guessFileName created by url -&gt; $url")
  
      val context = this
  
      downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
  
      val downloadUri = Uri.parse(url)
  
      val request = DownloadManager.Request(downloadUri)
      request.apply {
          setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE or DownloadManager.Request.NETWORK_WIFI)
          //setAllowedOverRoaming(true)
          setTitle(guessFileName)
          setDescription(guessFileName)
          setVisibleInDownloadsUi(true)
          allowScanningByMediaScanner()
          setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
  
          //request.setDestinationUri(Uri.fromFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)))
          setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, guessFileName)
  
          context.registerReceiver(receiver, IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE))
  
          downloadReference = downloadManager.enqueue(this)
      }
      System.out.println("LLLLLLLLLLLLLLLLLLL23 ");
  }
  </code></pre>