Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Data Persistence File Storage Reading from Internal Storage

Diego Marrs
Diego Marrs
8,243 Points

Could someone explain to me what I just wrote?

Hello.

I am having trouble understanding what I wrote down as I followed this video, could someone please explain this?:

public static void saveAssetImage(Context context, String assetName) {
        File fileDirectory = context.getFilesDir();
        File fileToWrite = new File(fileDirectory, assetName);

        AssetManager assetManager = context.getAssets();
        try {
            InputStream in = assetManager.open(assetName);
            FileOutputStream out = new FileOutputStream(fileToWrite);

            in.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Hmm, maybe I should have finished the beginner Android track...

Please help!

2 Answers

David Anton
PLUS
David Anton
Courses Plus Student 30,936 Points

You can use this function to save files from assets folder to the internal storage of you applications. Inline description:

public static void saveAssetImage(Context context, String assetName) {
     // get the internal storage folder
     File fileDirectory = context.getFilesDir();

     // get file in the internal storage folder with name = assetName 
     File fileToWrite = new File(fileDirectory, assetName);

     // get assets manager to access assets files 
     AssetManager assetManager = context.getAssets();
     try {
          // open input stream to read from the assets file
          InputStream in = assetManager.open(assetName);

          // open output stream to write to the internal storage file
          FileOutputStream out = new FileOutputStream(fileToWrite);

          // missing copy function from inputStream to outputStream
          /* copyFile(inputStream, outputStream); */

          // close the streams after finish copying
          in.close();
          out.close();
     } catch (IOException e) {
          e.printStackTrace();
     }
}
Diego Marrs
Diego Marrs
8,243 Points

Thanks! I think I'll finish the beginner track before I continue the intermediate track though :).

Sexual Potatoes
Sexual Potatoes
12,051 Points

I finished the Beginner Track and this didn't make sense to me at all

I would recommend doing their java course before this. I only knew what this mean due to prior C# knowledge from college but he definitely does not explain what he is doing very well if you have no clue about these terms.