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

Is there a way to manually create the package folder for an APK's .obb file?

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

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

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

File parent = obbFile.getParentFile();

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

外部 read/write 权限已设置并正常工作 read/write/创建其他 files/directories 没有问题。

我读过 但唯一的解决方案是将目标 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.
}

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