Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load a simple text file in Android Studio

Got a brand new project using Google's new Android Studio IDE.

I'm trying to load a simple text file using an InputStreamReader. I'm getting a file not found exception. Now there isn't any assets/ folder. I tried to create one and add my file at many different spots (at the root of the project, at the root of the .java file, etc...) I've tried to move the file around but still get the file not found.

Now that never was a problem using Eclipse as there is an assets folder created by any template.

Does anyone know where should the assets go to or how to load them?

Here is the code used, it fails at .open():

InputStream iS = resources.getAssets().open("bla.txt"); BufferedReader reader = new BufferedReader(new InputStreamReader(iS)); 

I also tried this code in Eclipse, it works and the file contents get loaded. So there's probably a step needed in Android Studio.

like image 873
Yohann T. Avatar asked May 29 '13 18:05

Yohann T.


People also ask

Where do I put text files in Android project?

Project window , press Alt-Insert , and select Folder->Assets folder . Android Studio will add it automatically to the correct location. And then you can add your assets or/txt files(whatever you want) on it.

How do I load an Android project?

Launch Android Studio, and click File > New > Import Project. Locate your project directory, click the build. gradle file you created above to select it, and then click OK to import your project.


1 Answers

  1. Step 1: Open in Name_Project-Name_Project.iml file.
  2. See the line : option name="ASSETS_FOLDER_RELATIVE_PATH" value="/src/main/assets"
  3. Step 2: Create a sub-folder "assets" in main folder.
  4. Step 3: Put file in this folder.
  5. Step 4: Load it. Done.
like image 56
FIT226557 Avatar answered Sep 29 '22 16:09

FIT226557