android - 如何使用存储访问框架android将文件保存到下载文件夹?

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

我正在从服务器下载文件,并保存到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()
                    }
                    )
    )
}

最佳答案

您可以在 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,而不是直接文件路径。

关于android - 如何使用存储访问框架android将文件保存到下载文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59390627/

相关文章:

Android Kotlin 有条件地调用不同且已弃用的构造函数

android - 如何在 android 10(Q) 中删除启动器图标

android - 媒体不允许主目录下载

java - 安卓 BottomSheetBehavior setState() NullPointerException

android - 无法使用 SherlockFragmentActivity 获取应用程序上下文

android - 我们可以在离线模式下使用 Google 的 Fused Location 服务吗?

file - 在 Kotlin 中递归列出文件

android - 使用无障碍服务

hibernate - 带有 JPA : default constructor hell 的 Kotlin

安卓运营商app e-sim激活码使用