如何访问Android10中的外部存储?

How to access external storage in Android 10?

根据Android 10 update,即使我们有

,也限制从外部存储读取和写入

WRITE_EXTERNAL_STORAGE

我想将我的 pdf 文件从内部存储(我的 android 应用程序私有)移动到 DOWNLOADS 文件夹。我尝试了很多方法,但没有任何效果,互联网上也没有任何与访问外部存储相关的帮助 android 10.

Android10 中的隐私更改 | Android 开发人员 https://developer.android.com

希望以下代码能帮助您解决问题并将任何文件从内部存储复制到外部存储。

fun copyPdfFrom(context: Context, sourceFile: File, destinationURIString: String) {
                val destinationURI = Uri.parse(destinationURIString)
                try {
                    val bufferedInputStream = BufferedInputStream(FileInputStream(sourceFile.absoluteFile))
                    val outPutStream = context.contentResolver.openOutputStream(destinationURI)!!
                    var len = 0
                    val brr = ByteArray(1024)

                    while ((bufferedInputStream.read(brr, 0, brr.size).also { len = it }) != -1) {
                        outPutStream.write(brr, 0, len)
                    }
                    outPutStream.flush()
                    outPutStream.close()
                    bufferedInputStream.close()
                }
                catch (e: Exception) {
                    e.printStackTrace()
                }
            }