Alexander  Shmuratko
Alexander Shmuratko

Reputation: 341

How do I address files in Asset folder?

I need to read "strings.json" file that lies in the "assets" folder of my Android project. But

File file = new File(filepath);
Logger.e(file.exists() ? "exists" : "doesn't exist");

says that the file doen't exist. I've tried the following variants of the filepath:

What's wrong?

Upvotes: 0

Views: 228

Answers (2)

matghazaryan
matghazaryan

Reputation: 5896

 AssetManager manager = getAssets();
        try {
            InputStream stream = manager.open(string+".xml");

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

Upvotes: 0

FKSI
FKSI

Reputation: 148

For read files in Assets:

        InputStream is = context.getAssets().open("strings.json");

        int size = is.available();

        byte[] buffer = new byte[size];

        is.read(buffer);

        is.close();

        fileResult = new String(buffer, "UTF-8");

In fileResult you should retrieve the content of your file.

Upvotes: 1

Related Questions