在Android上,在创建新的大文件之前和之后,我如何知道它会成功而又不会存储不足?

问题描述

背景

假设我要创建一个大文件(甚至其中的多个文件),但是我想确定自己会成功。

问题

我确定要这样做,我需要在要创建或修改的文件上使用freeSpaceusableSpace

但是,事实证明,Android比这更复杂:Android的缓存文件夹在需要时会减小大小,因此文档说我不应该依赖这些功能(“可用字节是提示,但不能保证”)。

另外不确定哪个更可信任。

因此,即使您从这些函数中获得了一些数字,并且尝试创建空间大于这些文件的文件,您也可能仍会成功,因为操作系统会为您清除一些缓存。这是一个很好的方法。

我发现的东西

was told应该使用这些:

getAllocatableBytes-返回的字节数是应用程序可以 分配,并认为系统愿意删除缓存。 然后,该应用可以使用以下方法分配空间: allocateBytes(FileDescriptor,long)allocateBytes(UUID,long)-分配请求的字节 用于应用程序(删除满足以下条件所需的所有缓存文件: 请求)。

getAllocatableBytes(UUID),而

Os.open(String,int,int)File.getAbsolutePath() 可能有助于获取FileDescriptor。

不确定这到底是什么意思,我还是尝试自己做。

给出一个我打算创建一个大文件的文件或文件路径,我想检查一下它有多少可用空间,以确定我是否真的可以在其中创建,创建并再次检查还剩多少空间:

val storageManager = ContextCompat.getSystemService(this,StorageManager::class.java)!!
val file = File(getExternalFilesDir(null),"largeFile.zip")
file.delete()
val uuidForPath = storageManager.getUuidForPath(file)
val allocatableBytes = storageManager.getAllocatableBytes(uuidForPath)
Log.d("AppLog","allocatableBytes :${Formatter.formatFileSize(this,allocatableBytes)} - $allocatableBytes bytes")
Log.d("AppLog","trying to create a new,large file:")
file.parentFile!!.mkdirs()
val fileDescriptor = Os.open(file.absolutePath,0)
val fileSize: Long = 2L * 1024L * 1024L * 1024L
storageManager.allocateBytes(fileDescriptor,fileSize)
Log.d("AppLog","file exists?${file.exists()} fileSize:${file.length()}")
val allocatableBytesAfterFileCreated = storageManager.getAllocatableBytes(uuidForPath)
Log.d("AppLog",allocatableBytesAfterFileCreated)} - $allocatableBytesAfterFileCreated bytes")

这向我显示了10.04 GB(10,042,085,376字节)的结果,但这很奇怪,因为在设置应用程序上显示了10.57 GB的可用空间。

因此,我不确定获取可用空间是否正确,因为它与操作系统所说的相距有点远。

此外,当应用程序达到了创建文件的部分(大小为2GB,应该足以容纳我得到的文件)时,它因以下异常而失败:

android.system.ErrnoException: open failed: ENOENT (No such file or directory)
    at libcore.io.Linux.open(Native Method)
    at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:254)
    at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
    at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7542)
    at android.system.Os.open(Os.java:412)

因此,即使直接创建文件,我也无法弄清楚该如何做。检查应该创建的文件(使用文件管理器应用程序),我看不到它,所以确实失败了。

我还尝试了allocateBytes的另一个功能,该功能改为获取文件的UUID,并得到相同的异常。

不仅如此,所有这些都需要Android O(API 26-Android 8.0)。我不知道该怎么办当然应该有一些类似的东西就足够了……

问题

看到我失败了,我想问一下如何正确做:

  1. 如何查询可以在给定文件路径上创建的文件的实际最大大小?或者,如果我打算创建多个文件,在我真正创建它们之前要知道可以创建它们?

  2. 即使在创建文件时,如何立即分配它们(不进行查询),以确保我确实成功了?

  3. 在较旧的Android版本(Android 8.0之前)上运行时,最好的解决方法是什么?

  4. 还有其他替代方法吗?我想我以前(在C / C ++课程中)听说过,有一种技巧可以通过将文件中的“游标”移动到要写入的最远位置来创建大文件。在Java / Kotlin上,或者在Android上,它可能也是一种替代选择?也许使用RandomAccessFile吗?

解决方法

1-检查可用空间然后创建文件可能不起作用,因为在检查可用空间和实际创建文件之间,某些其他应用程序可能会自己创建文件并占用您之前检查过的空间。我建议在success块中创建文件,并捕获与可用磁盘空间有关的异常并正确处理它们。

4-是的,您可以使用const ytdl = require('ytdl-core'); bot.on('message',async (msg) => { if (msg.author.bot) return; if (!msg.content.startsWith(prefix)) return; const args = msg.content.substring(prefix.length).split(' '); if (msg.content.startsWith(`${prefix}play`)) { const voiceChannel = msg.member.voice.channel; if (!voiceChannel) return msg.channel.send('You need to be in a voice channel.'); const permissions = voiceChannel.permissionsFor(msg.client.user); if (!permissions.has('CONNECT')) return msg.channel.send( "I don't have permissions to connect to a voice channel. help." ); if (!permissions.has('SPEAK')) return msg.channel.send( "I don't have permissions to speak in a voice channel" ); try { var connection = await voiceChannel.join(); } catch (error) { return msg.channel.send( `There was an error connecting to the voice channel ${error}` ); } const dispatcher = connection .play(ytdl(args[1])) .on('finish',() => { voiceChannel.leave(); }) .on('error',(error) => { msg.channel.send('There was an error: ' + error); }); dispatcher.setVolumeLogarithmic(5 / 5); } else if (msg.content.startsWith(`${prefix}stop`)) { if (!msg.member.voice.channel) return msg.channel.send('You need to be in a voice channel to stop the bot'); msg.member.voice.channel.leave(); return undefined; } }); 并使用try/catch方法设置其长度。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...