Game Development Reference
In-Depth Information
The AndroidFileIO Class
The original FileIO interface was lean and mean. It contained four methods: one to get an
InputStream for an asset, another to get an InputStream for a file in the external storage, a third
that returned an OutputStream for a file on the external storage device, and a final one that got
the shared preferences for the game. In Chapter 4, you learned how to open assets and files on
the external storage using Android APIs. Listing 5-1 presents the implementation of the FileIO
interface, based on knowledge from Chapter 4.
Listing 5-1. AndroidFileIO.java; Implementing the FileIO Interface
package com.badlogic.androidgames.framework.impl;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.os.Environment;
import android.preference.PreferenceManager;
import com.badlogic.androidgames.framework.FileIO;
public class AndroidFileIO implements FileIO {
Context context;
AssetManager assets;
String externalStoragePath;
public AndroidFileIO(Context context) {
this.context=context;
this.assets=context.getAssets();
this.externalStoragePath=Environment.getExternalStorageDirectory()
.getAbsolutePath()+File.separator;
}
public InputStream readAsset(String fileName) throws IOException {
return assets.open(fileName);
}
public InputStream readFile(String fileName) throws IOException {
return new FileInputStream(externalStoragePath+fileName);
}
public OutputStream writeFile(String fileName) throws IOException {
return new FileOutputStream(externalStoragePath+fileName);
}
 
Search WWH ::




Custom Search