Posted on 07 Dec 2018
This is the eighth article of the Android Game Programming series. In this article, I will discuss how to manage files in Android.
Android uses a file system similar to those on other platforms. In this section, we will see how to manage files in Android and we will use this knowledge later in our video game to save and read the highest scores made by players. This section assumes that you are already familiar with the basic concepts of java.io files and APIs.
All Android devices have two mass memories: internal and external. These names derive from the first days when Android was born when Android devices provided a built-in memory (internal) often large just to contain the operating system and a few dozen applications and then a removable memory (external) called ” SD “where to install other applications. These are the rules that usually apply to these two types of memories.
Using internal memory is convenient when you want data to be inaccessible to the user or other applications.
External memory is the ideal place for files that do not require special restrictions and can be shared by multiple applications. By default the applications are installed in the internal memory, you can specify the android: installLocation attribute in the manifest to install the application on the external memory. Users typically appreciate this option especially when the APK file size is large.
In order for your application to write to external memory, it must request the WRITE_EXTERNAL_STORAGE permissions in the manifest file:
There is a lot to write about Android files management on both internal memory and external memory, but the purpose of this article is to allow you to write your first video game in the shortest possible time and explaining only the concepts you will need for this purpose. Reading and writing files in Android uses the same Java java.io APIs that you already know: InputStream, OutputStream, FileInputStream, and FileOutputStream. The only problem is knowing on which path the SD card is mounted on. To know this, simply use the Environment.getExternalStorageDirectory().getAbsolutePath() API.
In this section, we will begin to add functionality to our “Hello World” application by including the ability to read and write files to and from external memory. We will use this file to read and write to a file the 5 highest scores reached with the video game. Since we haven’t implemented it yet we will use 5 fake scores such as 100, 80, 50, 30 and 10.
Here we will put the first brick for the construction of our framework that you can use to write many video games.
We create the package org.androidforfun.framework e org.androidforfun.framework.impl that will contain the source code of our game framework. The first one will contain the interfaces and the second their implementations. If you want, you can add implementations for other platforms in the future. In the first package we create the FileIO interface with only two methods:
These two methods will have to open an input and output stream to a file that will reside on the external memory. We create the AndroidFileIO class in the org.androidforfun.framework.impl package which will implement these two methods.
We now create a Settings class in the org.androidforfun.droids.model package that we will use to read and write the 5 scores on the file. The load method loads the 5 scores from the file and stores them in the static high scores array. The save method does the opposite. Finally, the addScore method adds new scores to the ranking. If a new score is higher than the first 5, the last one will be excluded and the new one will be inserted respecting the descending order.
We modify the file AndroidManifest.xml adding the permissions to write on the card External SD card.
We also change the layout file activity_my.xml in order to associate an id with the TextView, so that we can then modify it from Java code.
Finally, we modify the onCreate method of the MyActivity class by adding the following code after the call to setContentView.
The purpose of this code is to add 5 scores to the rankings: 5000, 4000, 3000, 2000 and 1000; and save them to disk. Then change the rankings by adding 5 higher scores. In the end, we reload the previously saved values to show them on video. On the video, the saved scores should be visible and not those added after even higher.