1

I have tried the solution suggested here: Load a simple text file in Android Studio among others.

I need to load the files from outside of an activity class, and the values be used in various other classes.

(Sidenote I don't necessarily have to have the files in the assets folder, they can be anywhere, as long as I could somehow load them).

Basically, it tells me to check a file named "app.iml" to have this line:

option name="ASSETS_FOLDER_RELATIVE_PATH" value="/src/main/assets"

Which it does.

After that, add the files to the "assets" directory.

I then try to load the file with:

File file = new File("IDs.txt");

and

Scanner in = new Scanner(file);

But I get a File Not Found exception.

No matter where I put the files, I am not able to load them. Any suggestions?

Community
  • 1
  • 1
Wilhelm Sorban
  • 1,012
  • 1
  • 17
  • 37
  • possible duplicate of [Is Asset folder read-only?](http://stackoverflow.com/questions/10562904/is-asset-folder-read-only) – hata Jul 26 '15 at 19:31
  • I am not trying to write, only read – Wilhelm Sorban Jul 26 '15 at 19:32
  • _Don't open the main project .iml file, but instead open the main module's .iml file. E.g. If your project is "MyProject" and what you actually run is "app", then open the "MyProject/app/app.iml" file. Hope this helps._ Did you try this? – Antrromet Jul 26 '15 at 19:32
  • Yes, it's the app.iml file that I checked. – Wilhelm Sorban Jul 26 '15 at 19:33
  • @Sorban Sorry, I mistook your question about 'add the files to the "assets directory"'. – hata Jul 26 '15 at 19:36
  • I still need to somehow load files in a class that is not an activity; The files can have whatever location, as long as I can load them. Still found no solution – Wilhelm Sorban Jul 26 '15 at 21:07

1 Answers1

0

Answer updated with copying file to app folder

You cant open file from assets with File f = new File. You should open assets like this:

File ids = new File(getExternalFilesDir("txt") + File.separator + "IDs.txt");
InputStream is = null;
    if (!ids.exists()) {
        AssetManager manager = getAssets();
        try {
            is = manager.open("IDs.txt");
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (is != null)
        try{
            OutputStream outputStream = new FileOutputStream(ids);
            byte buffer[] = new byte[1024];
            int length = 0;

            while ((length = is.read(buffer)) > 0) {
                outputStream.write(buffer, 0, length);
            }

            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    try {
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

Now you can set file or file_path to you class:

File ids = new File(getExternalFilesDir("txt") + File.separator + "IDs.txt");

To get a full path use ids.getAbsolutePath();

Wilhelm Sorban
  • 1,012
  • 1
  • 17
  • 37
Beyka
  • 1,372
  • 1
  • 10
  • 15
  • Thanks, but I need an other way to use File. Problem is that I cannot use getAssets, as my class is not an activity, with a context. – Wilhelm Sorban Jul 26 '15 at 19:44
  • You can copy asset from MainAcivity on first launch to app folder. In few minutes I will add this code to my answer – Beyka Jul 26 '15 at 20:06
  • I am still not able to scan a file using methods outside the MainActivity class, using this solution... – Wilhelm Sorban Jul 26 '15 at 20:34