Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8782301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:32:05+00:00 2026-06-13T20:32:05+00:00

I am trying to load a file from the assets folder, the file name

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:32:06+00:00Added an answer on June 13, 2026 at 8:32 pm

    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;
            }
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to load a list of filenames from a text file and then
I am trying to load an XML file from a different domain name as
I'm running jquery from a file. And I'm trying to load a svg file
I'm trying to load country names and their cities from an XML file in
I'm having trouble accessing springSecurityService from resources.groovy file, I'm trying to load user locale
Hei guys, I'm trying to load a long text from a .rtf file and
I am trying to load a TGA file in Android NDK. I open the
I am trying to load assets from an SWC into a SWF at compile
I'm trying to load a html page from the assets directory. I tried this,
i am trying load file after it created from /stext command line but its

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.