I am trying to load a file from the assets folder, the file name i want to be based on the current value of an int i (ie if i = 2 then open 2.txt and 2.jpg). I have the following code that deals with the asset manager side of things, and is working:
//link the image and text boxes to the xml
Image = (ImageView)findViewById(R.id.image);
Text = (TextView)findViewById(R.id.text);
loadDataFromAsset();
}
//actually load the text file and image file
public void loadDataFromAsset() {
//load the asset files themselves
try {
InputStream is = getAssets().open("1.txt");
//check file size
int size = is.available();
//create a buffer to handle it
byte[] buffer = new byte[size];
//send the data to the buffer
is.read(buffer);
//close the stream down
is.close();
//set the text we recovered to the TextView
Text.setText(new String(buffer));
}
catch (IOException ex) {
return;
}
//image file next
try {
InputStream ims = getAssets().open("1.jpg");
//load the image as drawable
Drawable d = Drawable.createFromStream(ims, null);
//set the drawable image to the imageview
Image.setImageDrawable(d);
}
catch (IOException ex) {
return;
}
}
I am new to java and dont really know how to move forward from here, how can i make the 1.jpg and 1.txt actually work based on the value of the int?
Thanks;
Andy
try this:
//actually load the text file and image file
public void loadDataFromAsset(int val) {
//load the asset files themselves
try {
InputStream is = getAssets().open(val + ".txt");
//check file size
int size = is.available();
//create a buffer to handle it
byte[] buffer = new byte[size];
//send the data to the buffer
is.read(buffer);
//close the stream down
is.close();
//set the text we recovered to the TextView
Text.setText(new String(buffer));
}
catch (IOException ex) {
return;
}
//image file next
try {
InputStream ims = getAssets().open(val + ".jpg");
//load the image as drawable
Drawable d = Drawable.createFromStream(ims, null);
//set the drawable image to the imageview
Image.setImageDrawable(d);
}
catch (IOException ex) {
return;
}
}