0

I'm trying to open up a file in Android Studio. I created an asset folder that has the path:

app_name\app\src\main\assets\file.csv 

When I try to open it using

FileReader file = new FileReader("assets/PlayerDB.csv"); 

I get an error that file is not found. To try and figure out the location of the root directory for getting files, I used the following:

File f = new File("test/test.txt"); String Path = f.getAbsolutePath(); Log.v("File Path",Path); 

However, this just prints "test/test.txt" to the log. Any ideas on how to get absolute path to work & how to open the file from the assets folder?

Thanks!

1
  • Assets are not files at runtime, so there is no File path to be obtained for them. Commented Sep 27, 2015 at 4:33

2 Answers 2

1

Place your PlayerDB.csv file in the /assets directory under the Android project. Use AssetManager class to access it.

AssetManager am = context.getAssets(); InputStream is = am.open("PlayerDB.csv"); 

And if your using it in a fragment

AssetManager am = getActivity().getAssets(); InputStream is = am.open("PlayerDB.csv"); 
Sign up to request clarification or add additional context in comments.

Comments

0

Simply as

getAssets().open("file.csv"); 

4 Comments

I get a "cannot resolve symbol getAssets" error when I try this. Any ideas?
getAssets() is a method on Context. If you are calling this in a fragment, try getActivity().getAssets().open("file.csv").
this is still giving me errors: 'FileReader file = getActivity().getAssets().open("PlayerDB.csv");' "unhandled exception java.io.ioexception"
Catch the exception to find out the cause. try {...} catch (Exception e) { Log.d("FOOBAR", e.toString() };

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.