How to create a folder under /Android/obb?

╄→гoц情女王★ 提交于 2019-12-24 10:09:11

问题


I'm trying to use expansion files and I found an issue that I'm not able to resolve.

It seems that you can access the /Android/obb folder and eventually delete the /Android/obb/my.package.name directory (tried with some file manager), but I cannot handle this situation whithin the app.

If I just let the Google Downloader library try to download the expansion file it will hang and error (for the missing folder), but it will start if I recreate the folder.

The strange thing is that I'm able to create the folder from other apps (the same file manager that I've used to see the directory) but not from mine!

I tried with

File f = new File(Environment.getExternalStorageDirectory(), "Android/obb/my.package.name");
f.mkdirs();

but it doesn't work.

I've the permissions in the manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

and I'm targeting api 23 and compiling with the 25.


回答1:


OP dropped the Target SDK to 22 and was able to avoid Dangerous permission work flow which was causing the issue.




回答2:


I had this same problem, but lowering the Target SDK was not an option.

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 and create it manually with mkdir()).

Excerpt from the documentation linked above:

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

So you can do something like:

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.



来源:https://stackoverflow.com/questions/45337142/how-to-create-a-folder-under-android-obb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!