有没有办法为APK的.obb文件手动创建包文件夹? [英] Is there a way to manually create the package folder for an APK's .obb file?

查看:780
本文介绍了有没有办法为APK的.obb文件手动创建包文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having an issue with an app: (rarely) a user will download the app from the play store but the obb/expansion file will fail to download. If the folder for the obb file is created (Android/obb/my.package.name), that's fine, I can manually download the obb file without a problem.

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

However, if the folder for the obb file was not created, attempting to create it with File::mkdir() or File::mkdirs() fails (returns false). Attempting to create and write to the obb file without a directory throws a FileNotFoundException.

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

File parent = obbFile.getParentFile();

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

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

External read/write permissions are set up and working correctly as we can read/write/create other files/directories with no issue.

我已阅读如何在/Android/obb下创建文件夹?,但是唯一的解决方案是将目标sdk降至22.

I've read How to create a folder under /Android/obb? but the only solution given to that is to drop the target sdk to 22.

还有其他人遇到这个问题并找到解决方案或解决方法吗?

Has anyone else come across this and found a solution or workaround?

推荐答案

The Context::getObbDir() method allows access to the expansion folder without the usual security rules. I found that, if the folder doesn't exist, getObbDir() creates it too (You can also double check create it manually with mkdir()).

上面链接的文档的摘录:

Excerpt from the documentation linked above:

返回主共享/外部存储目录,可以在该目录中找到此应用程序的OBB文件(如果有).请注意,如果应用程序没有任何OBB文件,则该目录可能不存在.

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.

就像getFilesDir()一样,在卸载应用程序时,这些文件将被删除,但是有一些重要的区别:

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

...从Build.VERSION_CODES.KITKAT开始,不需要权限来读取或写入此方法返回的路径. ...

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

Build.VERSION_CODES.N开始,不需要Manifest.permission.READ_EXTERNAL_STORAGE权限,因此不要在运行时要求此权限. ...

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

因此问题中的代码可以变成:

So the code in the question can become:

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.
}

注意::您可能需要读/写权限才能访问文件夹中的扩展文件.有关更多信息,请参见文档.

NOTE: You may need the read/write permissions to access the expansion files within the folder. See the documentation for more info.

这篇关于有没有办法为APK的.obb文件手动创建包文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆