简体   繁体   English

如何使用 Storage Access Framework android 将文件保存到下载文件夹?

[英]How to Save files to download folder using Storage Access Framework android?

I am downloading file from server ,and saving to download folder in android ,accessing file path by following way to save我正在从服务器下载文件,并保存到android中的下载文件夹,通过以下方式访问文件路径进行保存

 Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)

is depreceated in Android Q ,So I have to use Storage access framework .在 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()
                    }
                    )
    )
}

You can try DocumentFileCompat#createDownloadWithMediaStoreFallback() in Simple Storage :您可以在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

In Android 10, accessing files in Download directory requires URI, instead of direct file path.在 Android 10 中,访问Download目录中的文件需要 URI,而不是直接文件路径。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 android 存储访问框架一次创建多个文件 - How to create multiple files at once using android Storage Access Framework 如何在内部存储中下载和保存媒体文件 android java - how to download and save media files in internal storage android java Android:如何使用 SAF(存储访问框架)将文件移动到另一个文件夹,以及如何使用显示名称列出文件? - Android: How to move a file to another folder using SAF(storage access framework), and how to list file with display name? 如何在 Android 11 及更高版本上使用存储访问框架打开 Documents 文件夹? - How to open Documents folder using Storage Access Framework on Android 11 and higher? 使用 SAF(存储访问框架)从下载文件夹访问或列出非媒体内容 - Access or list non media content from Download folder using SAF (Storage Access Framework) 如何在内部存储中下载和存储android中的文件并访问它? - How to download & store files in android in internal storage and access it? 如何使用存储访问框架在 SD 卡中创建新文件夹? - How to create new folder in sd card by using Storage Access Framework? Android:如何访问内部存储文件夹中的所有文件? - Android: How to access all files in a folder in internal storage? 如何将文件保存在android存储设备上,写入文件并在以后访问它们? - How to save files on android storage, write to them, and access them later on? 如何下载内部存储中特定文件夹中的文件 - How to download the files in specific folder in internal storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM