Search code examples
androidlibgdxstorageunzip

Place to unzip files in Android


Because of apk maximum size 50MB, i decided to make apk expansion file. I know that I can get any file (inputstream) from this package but I am working in LibGDX, and this doesn't have option to load textures/music from inputstream. So, I must extract files, load texture/music and then I can delete file. I think that i need max 50MB space and i have two options:

UNZIP TO:

  1. ContextWrapper.getFilesDir() - it returns internal file, but I can't know how many files can I unzip there, because this storage is shared with all apps.

  2. getExternalStorageDirectory() - it returns external file, but according to website (developer.android.com) "This directory may not currently be accessible"

Which directory will be best to unpack and always avaliable?


Solution

  • If, as you tell us, here there are big quantities of data, so in order to improve user experience (not all phones have huge memory and using internal storage may cause your app is uninstalled) you should use external storage.

    You have to care about couple of things:

    • Add permissions to read / write
    • Check file availability starting app

    Adding permissions:

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

    Checking file availability
    And starting the app, be careful and check if external storage is available, and show message otherwise in order to avoid exceptions:

    Caution: External storage can become unavailable if the user mounts the external storage on a computer or removes the media, and there's no security enforced upon files you save to the external storage. All applications can read and write files placed on the external storage and the user can remove them.