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

时间:2012-11-03 08:57:39

标签: 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);
    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的值工作?

感谢;

安迪

3 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分: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;
        }


}

答案 2 :(得分:0)

试试这个

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