【问题标题】:download file from webview to internal storage从 webview 下载文件到内部存储
【发布时间】:2018-10-29 15:54:29
【问题描述】:

以下代码可以完美地下载外部存储中的 mp3 文件,但不能下载内部存储中的文件。许多智能手机没有外部存储。我能做些什么?如果需要的话,我不知道如何在 kotlin 中实现异步任务。

 mywebView.setDownloadListener(object : DownloadListener {
        override fun onDownloadStart(url: String, userAgent: String,
                                     contentDisposition: String, mimetype: String,
                                     contentLength: Long) {


            val request = DownloadManager.Request(Uri.parse(url))
            request.allowScanningByMediaScanner()
            request.setDescription("Download file...")
            request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimetype))

            request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE)
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) //Notify client once download is completed!
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, mimetype )
            val webview = getSystemService(DOWNLOAD_SERVICE) as DownloadManager
            webview.enqueue(request)

            Toast.makeText(getApplicationContext(), "Download avviato", Toast.LENGTH_LONG).show()

        }
    })

【问题讨论】:

  • 阅读thisthis。所以所有智能手机都有外部存储,并非所有智能手机都有 SD 卡。

标签: android kotlin


【解决方案1】:

目前无法测试代码,但应该如下所示:

 mywebView.setDownloadListener(object : DownloadListener {
        override fun onDownloadStart(url: String, userAgent: String,
                                     contentDisposition: String, mimetype: String,
                                     contentLength: Long) {


            val request = DownloadManager.Request(Uri.parse(url))
            request.allowScanningByMediaScanner()
            request.setDescription("Download file...")
            request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimetype))

            request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE)
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) //Notify client once download is completed!
            request.setDestinationInExternalPublicDir(Environment.getExternalStorageDirectory(), mimetype )
            val webview = getSystemService(DOWNLOAD_SERVICE) as DownloadManager
            webview.enqueue(request)

            Toast.makeText(getApplicationContext(), "Download avviato", Toast.LENGTH_LONG).show()

        }
    })

【讨论】:

  • 他要求的是内部存储,而不是外部存储
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-10
  • 2016-11-01
  • 1970-01-01
相关资源
最近更新 更多