Android access to resources outside of activity
Solution 1
If the folders are included in the Project Build Path, you can use ClassLoader access files under them outside the android.content.Context, for instance, from a POJO (in case if you don't want to pass a reference of android.content.Context):
String file = "res/raw/test.txt";
InputStream in = this.getClass().getClassLoader().getResourceAsStream(file);
- The res folder is included into Project Build Path by default by all Android SDK version so far.
- The assets folder was included into Project Build Path by default before Android SDK r14.
To add folders into Project Build Path, right click your project -- Build Path -- Configure Build Path, add your folder (for example, assets if using later SDK version) as a Source folder in build path.
Check out the similar question I answered before at here.
Solution 2
Android framework when compile your app create static class call:
your.namespace.project.R
you can statically call this class like:
your.namespace.project.R.string.my_prop
this.context.findViewById(your.namespace.project.R.id.my_id_prop);
Maybe you can access dynamic resources this way:
Resources res = this.context.getResources();
int id = res.getIdentifier("bar"+Integer.toString(i+1), "id", this.context.getPackageName());
Marc
Updated on September 15, 2022Comments
-
Marc over 1 year
I would like to know - are there ways to access android resources and/or assets files (or files from any other folder) outside of an Activity (without passing context)?
Can't access without context:
getResources().getIdentifier("resource_name", "drawable", getPackageName()); getAssets().open("file_in_assets_folder_name");
Throws Exception if not in Activity:
try { Class class = R.drawable.class; Field field = class.getField("resource_name"); Integer i = new Integer(field.getInt(null)); } catch (Exception e) {}
Also tried the below, but it complains file doesn't exist (doing something wrong here?):
URL url = new URL("file:///android_asset/file_name.ext"); InputSource source = new InputSource(url.openStream()); //exception looks like so 04-10 00:40:43.382: W/System.err(5547): java.io.FileNotFoundException: /android_asset/InfoItems.xml (No such file or directory)
-
Marc almost 12 yearsThank you, this works well for accessing a file. Could you also tell me if there is a way (without context) to access R.java contents? (my above question wasn't clear, by "android resources" I meant R.java).
-
yorkw almost 12 yearsWhat do you mean by "access R.java contents"? if you mean import/use public variable generated in R.java. Simply import com.example.R in you POJO class. Note that R.java is auto-generated from app resources and literately changed by SDK. I don't know what you want to achieve here but I don't see any point to use R.java outside android.content.Context.
-
Marc almost 12 yearsBy access I meant being able to obtain various information about the resources. For example, get int id value from name (similar to 1st line of code in question). I merely didn't want to pass in context if I didn't have to.
-
yorkw almost 12 yearsTry adding statement
import com.example.R
in POJO class. -
Marc almost 12 yearsFor some reason (probably since it's generated and has fancy methods for access) I was treating it in my mind as some mysterious object rather than just a class that has some public variables...
-
G_V over 9 yearsSo basically there's no way to sensibly make use of the android Resources system to store an array of values that will never be changed inside code in xml? For example, an array of types.