1

I don't know this is a duplicate question or not, but i tried to search similar question according to this.

I want to access the file that located outside /res folder programatically. I already know if we want to access /res folder, then we just call it's id like getString(), getDrawable() etc.

But in my case, I want to access anim_empty.json programatically. How to do that?

Sample image

Muhammad Faisal
  • 734
  • 10
  • 24
  • You can use `getAssets().open()` method from `Context` class which will provide you `InputStream` object. I.e. `context.getAssets().open("file name");` – Jeel Vankhede Feb 12 '19 at 05:58
  • 1
    Possible duplicate of [read file from assets](https://stackoverflow.com/questions/9544737/read-file-from-assets) – Basi Feb 12 '19 at 06:02

2 Answers2

1

Try following method for accessing JSON data:

public static String loadJSONFromAsset(Context mContext, String fileName) {
    String json;
    try {
        InputStream is = mContext.getAssets().open(fileName);
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        json = new String(buffer, "UTF-8");
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return json;
}

Modify the method according to your usage..

Hello World
  • 2,764
  • 1
  • 11
  • 23
1

In the truth, i just wanted to call the Lottie animation files, i thought that i need to write script like the answer above but all i need is just these (Getting Started With Animations in Android Using Lottie — Kotlin and enter link description here):

    lottieAnimationView = findViewById(R.id.empty_hstanim);
    lottieAnimationView.setAnimation("anim_empty.json");
    lottieAnimationView.playAnimation();

Thanks for the kind answer anyway!

Muhammad Faisal
  • 734
  • 10
  • 24