Game Development Reference
In-Depth Information
Listing 4-7. AssetsTest.java, Demonstrating How to Read Asset Files
package
com.badlogic.androidgames;
import
java.io.ByteArrayOutputStream;
import
java.io.IOException;
import
java.io.InputStream;
import
android.app.Activity;
import
android.content.res.AssetManager;
import
android.os.Bundle;
import
android.widget.TextView;
public class AssetsTest
extends
Activity {
@Override
public void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
TextView textView =
new
TextView(
this
);
setContentView(textView);
AssetManager assetManager = getAssets();
InputStream inputStream =
null
;
try
{
inputStream = assetManager.open("texts/myawesometext.txt");
String text = loadTextFile(inputStream);
textView.setText(text);
}
catch
(IOException e) {
textView.setText("Couldn't load file");
}
finally
{
if
(inputStream !=
null
)
try
{
inputStream.close();
}
catch
(IOException e) {
textView.setText("Couldn't close file");
}
}
}
public
String loadTextFile(InputStream inputStream)
throws
IOException {
ByteArrayOutputStream byteStream =
new
ByteArrayOutputStream();
byte
[] bytes =
new
byte
[4096];
int
len = 0;
while
((len = inputStream.read(bytes)) > 0)
byteStream.write(bytes, 0, len);
return
new
String(byteStream.toByteArray(), "UTF8");
}
}
We see no big surprises here, other than finding that loading simple text from an
InputStream
is rather verbose in Java. We wrote a little method called
loadTextFile()
that will squeeze all
the bytes out of the
InputStream
and return the bytes in the form of a string. We assume that