【问题标题】:How to Save files to download folder using Storage Access Framework android?如何使用 Storage Access Framework android 将文件保存到下载文件夹?
【发布时间】:2019-12-18 11:03:14
【问题描述】:

我正在从服务器下载文件,并保存到android中的下载文件夹,通过以下方式访问文件路径保存

 Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)

在 Android Q 中已弃用,所以我必须使用存储访问框架。

 fun downloadFile(url: String, originalFileName: String) {
    mDownloadDisposable.add(
            mRCloudRepository.downloadFile(url)
                    .flatMap(object : Function1<Response<ResponseBody>, Flowable<File>> {
                        override fun invoke(p1: Response<ResponseBody>): Flowable<File> {
                            return Flowable.create({
                                try {
                                    val file = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).absoluteFile, originalFileName)
                                    val sink = file.sink().buffer() 
                                    sink.writeAll(p1.body()!!.source())
                                    sink.close()
                                    it.onNext(file)
                                    it.onComplete()
                                } catch (e: IOException) {
                                    e.printStackTrace()
                                    it.onError(e)
                                }
                            }, BackpressureStrategy.BUFFER)
                        }

                    })
                    .subscribeOn(mIoScheduler)
                    .observeOn(mUiScheduler)
                    .subscribe(
                            {
                                cancelNotification()
                                val contentIntent: PendingIntent = PendingIntent.getActivity(mContext, NotificationBuilder.NOTIFICATION_DOWNLOAD_ID, getOpenFileIntent(it), PendingIntent.FLAG_CANCEL_CURRENT)
                                NotificationBuilder.showDownloadedNotification(mContext!!, 2, "Downloaded $originalFileName ", "", contentIntent)
                            }, {
                        cancelNotification()
                        it.printStackTrace()
                    }
                    )
    )
}

【问题讨论】:

  • 有什么问题吗?任何问题?请把所有信息放在你的帖子中。
  • @blackapps 更新了问题。我想知道如何使用 SAF 将文件保存在下载文件夹中。
  • 你应该把你的问题和疑问放在你的帖子中。不在 cmets 中。当然不仅仅是在主题上。
  • “我想知道如何使用 SAF 将文件保存在下载文件夹中”——这是不可能的,因为您无法控制内容的存储位置。用户可以。 ACTION_CREATE_DOCUMENT 将让 user 控制 user'suser's 设备或 user's 云服务上的位置用户希望你存储用户的内容。这可能是也可能不是名为Download
  • @CommonsWare .. 谢谢!我很好奇 gmail 是如何直接保存附件以下载文件夹的?没有任何类型的弹出窗口可以选择保存附件的位置吗?他们没有使用 SAF 吗?

标签: android kotlin android-10.0 storage-access-framework


【解决方案1】:

您可以在Simple Storage 中尝试DocumentFileCompat#createDownloadWithMediaStoreFallback()

val uri = DocumentFileCompat.createDownloadWithMediaStoreFallback(applicationContext, FileDescription(filename))
val output = uri?.openOutputStream(applicationContext)
val input = // get input stream from Response<ResponseBody>
// then, write input stream to output stream

在 Android 10 中,访问 Download 目录中的文件需要 URI,而不是直接的文件路径。

【讨论】:

    猜你喜欢
    • 2018-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-09
    • 2014-02-26
    • 1970-01-01
    相关资源
    最近更新 更多