gpt4 book ai didi

android - 有没有办法为 APK 的 .obb 文件手动创建包文件夹?

转载 作者:行者123 更新时间:2023-11-30 05:10:31 26 4
gpt4 key购买 nike

我的某个应用程序出现问题:(很少)用户会从 Play 商店下载该应用程序,但 obb/扩展文件将无法下载。如果创建了 obb 文件的文件夹 (Android/obb/my.package.name),那很好,我可以毫无问题地手动下载 obb 文件。

但是,如果未创建 obb 文件的文件夹,则尝试使用 File::mkdir()File::mkdirs() 创建它会失败(返回假)。尝试在没有目录的情况下创建和写入 obb 文件会引发 FileNotFoundException

File obbFile = new File("path/to/obb/file.obb");

File parent = obbFile.getParentFile();

if (!parent.exists() && !parent.mkdir()) {
// Failed to create directory
}

外部读/写权限已设置并正常工作,因为我们可以毫无问题地读/写/创建其他文件/目录。

我读过 How to create a folder under /Android/obb?但唯一的解决方案是将目标 sdk 降低到 22。

有没有其他人遇到过这个问题并找到了解决方案或解决方法?

最佳答案

Context::getObbDir()方法允许在没有通常的安全规则的情况下访问扩展文件夹。我发现,如果该文件夹不存在,getObbDir() 也会创建它(您也可以仔细检查是否使用 mkdir() 手动创建它)。

摘自上面链接的文档:

Return the primary shared/external storage directory where this application's OBB files (if there are any) can be found. Note if the application does not have any OBB files, this directory may not exist.

This is like getFilesDir() in that these files will be deleted when the application is uninstalled, however there are some important differences:

... Starting in Build.VERSION_CODES.KITKAT, no permissions are required to read or write to the path that this method returns. ...

Starting from Build.VERSION_CODES.N, Manifest.permission.READ_EXTERNAL_STORAGE permission is not required, so don’t ask for this permission at runtime. ...

所以题中的代码可以变成:

File obbDir = getObbDir();

if (null == obbDir) {
// Storage is not available
} else if (!obbDir.exists() && !obbDir.mkdir()) {
// Failed to create directory. Shouldn't happen but you never know.
}

注意:您可能需要读/写权限才能访问文件夹中的扩展文件。有关详细信息,请参阅文档。

关于android - 有没有办法为 APK 的 .obb 文件手动创建包文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53830611/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com