根据 Android 10 update , 即使我们有,也限制从外部存储读写
WRITE_EXTERNAL_STORAGE
我想将我的 pdf 文件从内部存储(我的 android 应用程序私有(private))移动到 DOWNLOADS 文件夹。我尝试了很多方法,但没有任何效果,互联网上也没有任何与访问 Android 10 中的外部存储相关的帮助。
Android 10 中的隐私变化 |安卓开发者 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()
}
}
关于android - 如何在 Android 10 中访问外部存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58372784/