Android 写入文件存储
我想将文件写入外部存储,当我查看Android 写入文件存储,android,file,kotlin,storage,android-external-storage,Android,File,Kotlin,Storage,Android External Storage,我想将文件写入外部存储,当我查看context.getExternalFilesDir(Environment.DIRECTORY\u DOWNLOADS)的usableSpace时,它说只有515.74MB 这是目录的典型usableSpace大小吗?如果您需要更多的存储空间来写入,您将如何处理这种情况 private fun getStorageDirectory(): File { return if (isExternalStorageWritable()) {
context.getExternalFilesDir(Environment.DIRECTORY\u DOWNLOADS)
的usableSpace
时,它说只有515.74MB
这是目录的典型usableSpace
大小吗?如果您需要更多的存储空间来写入,您将如何处理这种情况
private fun getStorageDirectory(): File {
return if (isExternalStorageWritable()) {
context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)
} else {
context.filesDir
}
}
private fun getUsableSpace(): Long {
return getStorageDirectory()?.usableSpace ?: 0L
}
更新
在模拟器上测试时,不同存储目录的usableSpace如下所示
DEBUG ~~ [context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).usableSpace 515739648] 515.74MB
DEBUG ~~ [context.getExternalFilesDir(null).usableSpace 515739648] 515.74MB
DEBUG ~~ [context.filesDir.usableSpace 515739648] 515.74MB
DEBUG ~~ [context.externalCacheDir.usableSpace 515735552] 515.74MB
DEBUG ~~ [context.cacheDir.usableSpace 515735552] 515.74MB
在Google Pixel XL上测试时,不同存储目录的可用空间如下所示
DEBUG ~~ [context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).usableSpace 79697793024] 79.70GB
DEBUG ~~ [context.getExternalFilesDir(null).usableSpace 79697793024] 79.70GB
DEBUG ~~ [context.filesDir.usableSpace 79697793024] 79.70GB
DEBUG ~~ [context.externalCacheDir.usableSpace 79697764352] 79.70GB
DEBUG ~~ [context.cacheDir.usableSpace 79697760256] 79.70GB
因此,我想在真实的设备上,您有更多的写入空间。您可以共享堆栈跟踪吗?你在模拟器或手机上试过这个吗?大多数模拟器的内部存储空间较少。如果不想向用户显示文件,是否尝试在内部存储中写入文件?目录没有空间大小。只有一个分区大小。您可以写入分区上的每个目录,直到分区“已满”。您提到的目录主要位于不同的分区上。您没有给出两者的大小。@MADLAD这498MB是在模拟器上的。我将报告真实设备上的数字。没有堆栈跟踪。这不是一个错误。但是,我会检查可用空间,如果没有足够的可用空间,我就不会写入文件。