Question

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

Was it helpful?

Solution

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


}

OTHER TIPS

Try to use this for text

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

for image try this

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();
}

try like this

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top