1

我正在尝试从资产文件夹加载一个文件,我希望文件名基于 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 的值实际工作?

谢谢;

安迪

4

3 回答 3

1

尝试将其用于文本

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();
}
于 2012-11-03T09:15:39.223 回答
1

试试这个:

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


}
于 2012-11-03T13:37:17.123 回答
0

试试这样

InputStream is = getAssets().open(i+".txt");

于 2012-11-03T09:05:29.063 回答