0

After upgrading to Android gradle plugin 2.2.0 (and Android Studio 2.2) I've been having trouble reading from a zip file which is located at app/src/main/assets/magic_info.zip It used to work fine with plugin versions 2.1.3 and prior but now it throws the following exception

java.io.FileNotFoundException: magic_info.zip
 at android.content.res.AssetManager.openAsset(Native Method)
 at android.content.res.AssetManager.open(AssetManager.java:334)
 at android.content.res.AssetManager.open(AssetManager.java:308)

for some reason it can't find the file from assets which is otherwise there.

Here is my code that used to work in the past.

   public static ZipInputStream getZipInputStream(Context context)
{
    try
    {
        return new ZipInputStream(context.getAssets().open("magic_info.zip"));
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    return null;
}

Any idea what's wrong with this? Am I missing something?

ThanosFisherman
  • 5,626
  • 12
  • 38
  • 63

2 Answers2

0

Replace this:

return new ZipInputStream(context.getAssets().open("magic_info.zip"));

with this:

return new ZipInputStream(getResources().getAssets().open("magic_info.zip"));
ᴛʜᴇᴘᴀᴛᴇʟ
  • 4,466
  • 5
  • 39
  • 73
0

OK I've figured it out and it's kind of strange.

As of Android Studio 2.2 and gradle plugin 2.2 you are required to have the android SDK which corresponds to your testing device along with the target SDK installed on your machine if you want things to work properly.

Now I may target API 24 but I test my app to a device running lollipop (API 21). Once I installed SDK 21 the file is being read normally.

ThanosFisherman
  • 5,626
  • 12
  • 38
  • 63