Java – openRawResource() always throws a NotFoundException

openRawResource() always throws a NotFoundException… here is a solution to the problem.

openRawResource() always throws a NotFoundException

I want to load a keystore in my Android app. I have copied the key (truststore.bks) into the folder res/raw. The SDK from Android generates R .java with trustsotre entries. Try using in the app:
Resources.getSystem().openRawResource(R.raw.truststore);

But I always get an exception:
android.content.res.Resources$NotFoundException: Resource ID #0x7f040000

I looked at the apk file and found that the truststore.bks file is in the folder res/raw/.

What is my error?

Solution

The problem is that you are using system-level resources by calling Resources.getSystem. Specifically, the documentation states that this does not provide access to application resources. You need to call getResources on the Context object in your application – all your activities are a Context object because they inherit from the Context.

Related Problems and Solutions