1

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.

Enrichman
  • 11,157
  • 11
  • 67
  • 101
  • Do you get the pop up to accept the permission on runtime ? Have you implemented the dangerous permission flow? – android_Muncher Jul 26 '17 at 20:53
  • Nope, that's why I'm targeting the api 23. I just don't want to mess with all the hassles of the permissions. Do I have to? – Enrichman Jul 26 '17 at 20:54
  • @android_Muncher f**, I was thinking that I needed to target 23, but actually they INTRODUCED the runtime permissions with 23. :/ Now targeting the 22 it works. Do you want to answer the question and get the points? You actually helped me on spotting the issue. :) – Enrichman Jul 26 '17 at 20:59
  • give me some credit :) – android_Muncher Jul 26 '17 at 21:03
  • 1
    Thanks, I've had to wait few minutes in order to accept the answer. :) – Enrichman Jul 26 '17 at 21:13

2 Answers2

2

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

android_Muncher
  • 1,057
  • 7
  • 14
  • At this time, Google is making it impossible to use Target SDK to 22 to avoid Dangerous Permissions workflow. – Cory Trese Dec 17 '18 at 15:57
1

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.

Community
  • 1
  • 1
Alex Meuer
  • 1,621
  • 3
  • 26
  • 37