Here's the core of my ProgressBar class:
package nttu.edu.activities;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Stack;
import nttu.edu.R;
import nttu.edu.graphics.Art;
import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ProgressBar;
public class NewLoadingActivity extends Activity
{
private ProgressBar bar;
private AssetManager assetManager;
private Load loading;
private class Load
{
public Stack<byte[]> stack;
public Stack<Bitmap> results;
public Handler handler;
public int totalByteSize;
public int currentByteSize;
private final String[] list =
{ "art/sprites.png" };
public Load()
{
stack = new Stack<byte[]>();
results = new Stack<Bitmap>();
handler = new Handler();
totalByteSize = 0;
currentByteSize = 0;
}
public void loadBar()
{
try
{
for (int i = 0; i < list.length; i++)
{
byte[] bytes = readFromStream(list[i]);
stack.push((byte[]) bytes);
totalByteSize += bytes.length;
}
}
catch (IOException e)
{
throw new RuntimeException(e);
}
bar.setMax(totalByteSize);
}
public void startHandler()
{
handler.post(new Runnable()
{
public void run()
{
while (currentByteSize < totalByteSize)
{
try
{
Thread.sleep(1000);
bar.setProgress(currentByteSize);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
});
}
public void startLoad(){
while (stack.size() > 0){
byte[] bytes = (byte[]) stack.pop();
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
if (bitmap != null)
currentByteSize += bytes.length;
results.push((Bitmap) bitmap);
}
sort();
finish();
}
//This is the place to load specific assets into a class.
private void sort(){
Art.sprites = (Bitmap) results.pop();
}
private byte[] readFromStream(String path) throws IOException
{
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length = 0;
InputStream input = assetManager.open(path);
while (input.available() > 0 && (length = input.read(buffer)) != -1)
output.write(buffer, 0, length);
return output.toByteArray();
}
}
public void onCreate(Bundle b)
{
super.onCreate(b);
this.setContentView(R.layout.progressbar);
assetManager = this.getAssets();
loading = new Load();
//bar = new ProgressBar(this);
bar = (ProgressBar) this.findViewById(R.id.loadingBar);
loading.loadBar();
loading.startHandler();
loading.startLoad();
}
public void finish()
{
Intent intent = new Intent(this, BaseActivity.class);
intent.putExtra("Success Flag", Art.sprites != null);
this.setResult(RESULT_OK, intent);
super.finish();
}
}
So far, I can only load bitmaps by adding their paths into the sort() function.
The reason I can only load bitmaps is that I don't know how to differentiate Bitmap loading, Sound loading, and Resource loading, but I wanted to put all of the things needed to be loaded into 1 large class. I just don't know how to split them up.
I tried splitting the required files up by directory names, or sorting them into subdirectories of their own, like so:
But then I would find myself stuck on finding a new solution to recursive directory listing and stuff, and still wouldn't be able to fix it. I've been tackling the problem for the past 2 days, but nothing came up good.
Here is my result for that, so to prove that I've really been doing my homework:
public void loadStack(AssetManager manager, String path, int level) {
try {
String[] list = manager.list(path);
if (list != null) {
for (int i = 0; i < list.length; i++) {
if (level >= 1) loadStack(manager, path + "/" + list[i], level + 1);
else if (level == 0) loadStack(manager, list[i], level + 1);
else {
byte[] byteBuffer = readFromStream(path);
assetStack.push(byteBuffer);
totalByteSize += byteBuffer.length;
}
}
}
}
catch (IOException e) {
Log.e("Loading", "Occurs in AssetLoad.loadStack(AssetManager, String, int), file can't be loaded: " + path);
throw new RuntimeException("Couldn't load the files correctly.");
}
}
I've been continuing to do more researching on how to split up different file types in a loading screen, but there aren't any questions on Stack Overflow regarding how to load different files altogether.
With that, I decided to come up with a simple, rough, and bad answer on doing this, and that is to create all resources in bitmaps, and sacrifice sound files for a game application. And to be honest, I don't want to do that.
Please help me, such as giving me hints, tips, or whatever you've got in your sleeves. What should I do, in order to load all sorts of file types in my progress bar? What do I need to look for?
Thanks in advance.
Here's how to create a ProgressBar for arbitary files.