Java Question RenderScript rs files

urikupfer

Member
Licensed User
Longtime User
Hi erel
I don’t have a lot of experience in java projects.
The statement in eclips android project to call rs file is "script = new ScriptC_snow(mRS, getResources(), R.raw.snow);",
The rs file is "snow.rs", please, what are the equivalent statements in java project for the: " getResources()" and " R.raw.snow".
uri
 

warwound

Expert
Licensed User
Longtime User
In your java code you can get a reference to the application's Resources object using:

B4X:
Resources resources=BA.applicationContext.getResources();

R.raw.snow is an integer resource identifier, you can get that identifier using:

B4X:
int resourceId=resources.getIdentifier("snow", "raw", BA.packageName);

To put that together and answer your question you'd use:

B4X:
Resources resources=BA.applicationContext.getResources();
int resourceId=resources.getIdentifier("snow", "raw", BA.packageName);
script = new ScriptC_snow(mRS, resources, resourceId);

If the resource id is not found - for some reason the raw resource 'snow' has not been compiled into the application or it has not been compiled into the application resources then resourceId will have a value of zero.

So add some debug error checking:

B4X:
Resources resources=BA.applicationContext.getResources();
int resourceId=resources.getIdentifier("snow", "raw", BA.packageName);
if(resourceId==0){
   BA.LogError("Failed to find resource id");
} else {
   script = new ScriptC_snow(mRS, resources, resourceId);
}

Martin.
 
Top