Skip to content Skip to sidebar Skip to footer

Java.net.url.getfile() On A Null Object Reference

So I need to pass a key (.p12) file SERVICE_ACCT_KEYFILE which is stored directly under the res folder in my Android project. I am setting the setServiceAccountPrivateKeyFromP12Fil

Solution 1:

In order to access a resource in an Android application, it should be located either in src/main/res/raw or src/main/assets directory. Android doesn't load resources via classloader.

If the p12 file, say, keyfile.p12 is in src/main/res/raw directory, it can be accessed this way:

InputStreamrawIs= getResources().openRawResource(R.raw.keyfile);

If keyfile.p12 is in src/main/assets directory:

InputStreamassetIs= getAssets().open("keyfile.p12");

Then you'll need to save the InputStream into a File. You can create a file inside your app internal storage with new File(getFilesDir(), "keyfile.p12") or any method available in Context class that returns a directory for file storage instead of getFilesDir(). I leave the file copying code as an exercise :)

All of getResources(), getAssets() and getFilesDir() are instance methods of Context class.

Some reference, and explanation of the difference between raw and assets: https://developer.android.com/guide/topics/resources/providing-resources.html

Post a Comment for "Java.net.url.getfile() On A Null Object Reference"