XYseven
XYseven

Reputation: 525

Where does Android look for resource files at run time?

I am trying to open a file located within my android studio project @ res/drawable/conan_obrian.png. However, a java.io.FileNotFoundException was thrown. I have tried different pathname combinations with no results.

this snippet is throwing the exception:

    InputStream is;
    byte[] buffer = new byte[0];
    try {
        final AssetManager assetMgr = context.getResources().getAssets();
        is = assetMgr.open("drawable/conan_obrian.png");
        buffer = new byte[is.available()];
        is.read(buffer);
    } catch (IOException e) {
        e.printStackTrace();
    }
    serverAPI.register(userName, Base64.encodeToString(buffer,Base64.DEFAULT).trim(), myCrypto.getPublicKeyString());

My project's file tree structure

Upvotes: 1

Views: 233

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191710

Your code would be correct if you had an assets/drawable folder, but you are trying to load a resource, so that would be R.drawable.conan_obrian.

You can use that id to setResourceDrawable on an ImageView, for example.

Or you can get a Drawable object using that ID with

Drawable d = getResources().getDrawable(R.drawable.conan_obrian) 

Anything under the res/ directory gets loaded into a long list of integers in the R.java file.

Upvotes: 3

Related Questions