How to read file that is inside the . apk?

Asked

Viewed 574 times

1

I’m having a little problem developing an app. I need to read the information of a file that is inside the . apk of my app, ie. That file you need to read is inside my app and not outside it.

This file is inside the same package where the java class is trying to read it, but every time I run, it happens:

Exception Caused by: android.system.Errnoexception: open failed: ENOENT (No such file or directory)

I’m trying to get the file this way:

FileInputStream fileInputStream = new FileInputStream("teste.js");

The error is going in that line. Does anyone know how I can fix it? How to get the file path correctly?

Thank you.

  • To my knowledge you can only access files that are underneath the folder \res.

  • I tried to put the file in libs folder and even then it didn’t work. :(

1 answer

1

Hello!

You must put in the Assets folder!

Here’s an example of how to load the file:

 /**
 * Carrega um arquivo txt 
 */
private void load(){
      try {
         final InputStream inputStream = getAssets().open("dados.txt");
         final InputStreamReader inputreader = new InputStreamReader(inputStream); 
            BufferedReader buffreader = new BufferedReader(inputreader); 
            final List<String> itens = new ArrayList<String>(0);
            String line = "";
                while ((line = buffreader.readLine()) != null){

                    itens.add(item);
                }

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.