2

I have a project in Android Studio and I want to read data from a file, but I want to avoid using an absolute path. I want to write something similar to File("FileName.extension"); without any /../folder stuff.

So my question is where in the hierarchy is this folder?

EDIT: I am trying to read the file outside an Activity. I have a subclass where I need the data so I am trying to read it there.

Noobs DeSroobs
  • 253
  • 1
  • 6
  • 24

3 Answers3

2

Place your file in Asset folder, then access it via AssetManager

AssetManager assetManager = getResources().getAssets();
InputStream inputStream = assetManager.open("MyFile.txt");
//to get the content of the file as String
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder out = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
     out.append(line);
}
String myString=out.toString();
reader.close();
makata
  • 2,188
  • 2
  • 28
  • 23
  • I need to read the entire contents into a single string and I tried using ` new Scanner(new File("VertexShader.txt")).useDelimiter("\\Z").next();`. Can I use the asset manager to get me a file reference? EDIT: The openFD function returns an AssetFileDescriptor, but can i use that with as a File reference? – Noobs DeSroobs Jul 15 '15 at 11:52
  • @Noobs, I edited the answer. From the AssetManager you can get InputStream and you can manipulate the stream as u want. – makata Jul 15 '15 at 12:04
  • Problem is I cant use the getResources() function (probably) because I am not in the activity class. Is there a way to do this in a non-activity class without sending the context along for the ride? – Noobs DeSroobs Jul 15 '15 at 12:05
  • Sorry, you can't! you have to have a reference to context (or ApplicationContext.) But, why don't you pass the context as a parameter for the method? Is there any special condition? – makata Jul 15 '15 at 12:12
  • Oh, ok. Well, I guess I will just send the context down then. It is not the end of the world. Thanks a lot! I need it once for reading a textfile in the constructor of my OpenGL handler. I need to read the shader code into a string so I can compile it for the graphics card. – Noobs DeSroobs Jul 15 '15 at 12:15
0

You can use asset folder to store files. For example, from an activity, you can use the method

getAssets().open("filename.txt")));

To open as InputStream the file named filename.txt. For more info read

read file from assets

and

Where do I place the 'assets' folder in Android Studio?

Community
  • 1
  • 1
xcesco
  • 4,690
  • 4
  • 34
  • 65
0

It is not actually good practice to avoid any paths, since the Android Gradle build system requires you to place all assets under the asset folder, for the sake of proper organization.

But in case you really want to avoid using an absolute path, you can either go with makata's answer, or you can do this -

  • Open your project's root directory. Let us say your project name is MyAndroidProject.

  • Check out the file MyAndroidProject.iml. Within the <configuration> tag, you should see

<option name="ASSETS_FOLDER_RELATIVE_PATH" value="/src/main/assets" />.

In case you don't see any such option, go to /MyAndroidProject/app/ and open app.iml.

  • Change /src/main/assets to /src/main/java/com/mypackage/subpackage/myandroidproject.

Now you can place all your assets within the package of your application, and all files will be read from there as you wanted. But then you'll be messing around with files you don't really want to mess with, and that can lead to problems IMO.

Kunal Chawla
  • 1,236
  • 2
  • 11
  • 24
  • I see. Well, I am going to load code from a file and I do not want to do programming with string concatination. I dont mind putting it in the asset folder, but I do not see why it is so difficult to read a string of bytes from a file and save it in a string. In C you could do this in one line. With this system I have to write 10+ and I find it to be a waste. – Noobs DeSroobs Jul 15 '15 at 12:10
  • Yeah I agree things were way simpler back then, but things have changed. Today, well-organized, good-quality code is more important than ever. And for that we developers will have to make some sacrifices. – Kunal Chawla Jul 15 '15 at 12:17