我正在尝试从资产文件夹加载一个文件,我希望文件名基于 int i 的当前值(即如果 i = 2,则打开 2.txt 和 2.jpg)。我有以下处理资产管理器方面的代码,并且正在工作:
//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;
}
}
我是 java 新手,真的不知道如何从这里继续前进,我怎样才能使 1.jpg 和 1.txt 根据 int 的值实际工作?
谢谢;
安迪