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 SharedPreferences 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;
    }
    @Override
    public InputStream readAsset(String fileName) throws IOException {
        return assets.open(fileName);
    }

    @Override
    public InputStream readFile(String fileName) throws IOException {
        return new FileInputStream(externalStoragePath + fileName);
    }


    @Override
    public OutputStream writeFile(String fileName) throws IOException {
        return new FileOutputStream(externalStoragePath + fileName);
    }

    public SharedPreferences getPreferences() {
        return PreferenceManager.getDefaultSharedPreferences(context);
    }
}

Everything is straightforward. We implement the FileIO interface, store the Context, which is the gateway to almost everything in Android, store an AssetManager, which we pull from the Context, store the external storage's path, and implement the four methods based on this path. Finally, we pass through any IOExceptions that get thrown so we'll know if anything is irregular on the calling side.

Our Game interface implementation will hold an instance of this class and return it via Game.getFileIO(). This also means that our Game implementation will need to pass through the Context in order for the AndroidFileIO instance to work.

Note that we do not check if the external storage is available. If it's not available, or if we forget to add the proper permission to the manifest file, we'll get an exception, so checking for errors is implicit. Now, we can move on to the next piece of our framework, which is audio.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.26.158