0

Sorry for the noob question. I am still pretty new to Android development.

I am currently working on an Android app that was designed to pull some images from a zip file sitting in the Assets directory. The original goal, it seems, was to have the app check a server to see if the zip had been updated, and then download the new zip in the case that it had.

The problem is that the Assets directory, if I understand correctly, is read only.

The thing is- I still need to be able to follow their original design of checking the server for an update to this file and downloading the new one. Since the file is hefty in size, simply downloading it to memory every time isn't really feasible. So, with that said, I need somewhere static to download it to so that I can tell the app at start up to look there first, and if that is empty then use the backups in Assets.

Is the root folder of the app the best place to download this file, which will remain indefinitely, possibly be replaced often and be read from on every run? Or is that against standard design procedures?

Thanks!

user1548103
  • 869
  • 2
  • 12
  • 25

1 Answers1

0

like you sad, asset folder is read only. You can't save anything there programmatically.

If you want save some data you have to create folder of your app on mobile storage. For example data/yourApkName then save some file there.

String content = "hello world";
File file;
FileOutputStream outputStream;
try {
    file = new File(Environment.getExternalStorageDirectory(), "youtApkName");

    outputStream = new FileOutputStream(file);
    outputStream.write(content.getBytes());
    outputStream.close();
} catch (IOException e) {
    e.printStackTrace();
}

You also can create cache/temporary files.

Saving cache files

To open a File that represents the external storage directory where you should save cache files, call getExternalCacheDir(). If the user uninstalls your application, these files will be automatically deleted.

Similar to ContextCompat.getExternalFilesDirs(), mentioned above, you can also access a cache directory on a secondary external storage (if available) by calling ContextCompat.getExternalCacheDirs().

Tip: To preserve file space and maintain your app's performance, it's important that you carefully manage your cache files and remove those that aren't needed anymore throughout your app's lifecycle.

If you want sane files on storage you have to add this to your manifest

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

If you are looking after method to download files you have to look here.

Community
  • 1
  • 1
Eliasz Kubala
  • 3,836
  • 1
  • 23
  • 28