Android 基于int值/文件名加载文件

Android 基于int值/文件名加载文件,android,asset-management,Android,Asset Management,我试图从assets文件夹加载一个文件,我希望文件名基于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); loadDataFromAss

我试图从assets文件夹加载一个文件,我希望文件名基于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新手,不知道如何从这里开始前进,如何根据int值使1.jpg和1.txt真正工作

谢谢,

安迪

像这样试试


InputStream is=getAssets().open(i+“.txt”)

尝试将此用于文本

InputStream is = getResources().getAssets().open("yourINTvalue.txt");
String textfile = convertStreamToString(is);
Text.setText(textfile);

public static String convertStreamToString(InputStream is)
        throws IOException {
        Writer writer = new StringWriter();

        char[] buffer = new char[2048];
        try {
            Reader reader = new BufferedReader(new InputStreamReader(is,
                    "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            is.close();
        }
        String text = writer.toString();
        return text;
}
要获得图像,请尝试此

InputStream bitmap=null;
try {
bitmap=getAssets().open("yourINTvalue.png");
Bitmap bit=BitmapFactory.decodeStream(bitmap);
img.setImageBitmap(bit);
} catch (IOException e) {
e.printStackTrace();
} finally {
bitmap.close();
}
试试这个:

//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;
        }


}

非常好,谢谢。我只需要加上private int val=1;在顶部初始化,然后将loadDataFromAsset()更改为loadDataFromAsset(val);在oncreate下。谢谢