In Samsung Galaxy S4 I face a strange problem. I 've written this code to get all the resource drawables from other applications. It works fine in emulator and in two other devices I've tested it but in S4 it throws an out of memory error after throwing MANY exceptions with this message:
ResourceType: Failure getting entry for 0x7f020292 (t=1 e=658) in package 0 (error -75)
It successfully fetches some resources, fails at others and eventually crashes. I don't understand why the out of memory error occures since the drawables aren't really loaded. Is there any way to stop this from happening ?
Also...this doesn't happen for all applications. It's kind of random.
class IconItem{
public int resId;
Drawable d;
String resName;
public IconItem(String resName, int resId){
this.resId=resId;
this.resName=resName;
}
}
public void getResourceDrawablesForPackageName(String packageName){
String resourcesClassName = packageName + ".R";
String drawableClassName = resourcesClassName + "$drawable";
ArrayList<IconItem> icons=new ArrayList<IconItem>();
try {
Context otherAppContext;
otherAppContext = createPackageContext(packageName, Context.CONTEXT_INCLUDE_CODE|Context.CONTEXT_IGNORE_SECURITY);
Resources resources = otherAppContext.getResources();
Class<?> externalR = otherAppContext.getClassLoader().loadClass(resourcesClassName);
Class<?>[] resourceTypes = externalR.getDeclaredClasses();
for(Class<?> resourceType : resourceTypes ){
if(resourceType.getName().equals(drawableClassName)){
for(Field field : resourceType.getFields()){
try{
int id = field.getInt(resourceType);
Drawable drawable = resources.getDrawable(id);
if(drawable!=null){
IconItem item = new IconItem(packageName+":drawable/"+field.getName(),field.getInt(resourceType));
item.d=drawable;
icons.add(item);
}
}catch(Exception e){Log.e("ERROR GETTING ICON","ERROR GETTING ICON");}
}
}
}
} catch (Exception e) { }
}