0

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) {  } 
}
jomuller
  • 1,032
  • 2
  • 10
  • 19
Anonymous
  • 4,470
  • 3
  • 36
  • 67
  • Well, this code won't be super-reliable in general, in that there is no requirement that there be a class named `R` in a Java package that is the value of an app's application ID (which is what I assume `packageName` is). That being said, on what line are you getting that error message? – CommonsWare Mar 08 '15 at 23:25
  • The "Failure getting entry" errors are thrown at Drawable drawable = resources.getDrawable(id); from what I saw using log messages. The id is allways retrieved successfuly – Anonymous Mar 08 '15 at 23:27
  • Hmmmm... it's possible that through some app-optimization stuff, the `R` class has a reference to a drawable but there is no actual drawable in the APK itself. So long as the app doesn't try using that drawable, the app is fine, but code like yours will encounter problems. As to why you are running out of memory, it's possible that this is unrelated to these errors, and you have some other memory-management issue. If you call `getResourceDrawablesForPackageName()` for lots of packages, I would expect you to run out of memory eventually. – CommonsWare Mar 08 '15 at 23:32
  • That's why I only do this on demand for a certain packageName that the user selects from an app list. I don't really care if I get all the drawables available..The big prolbem is the out of memmory error. which seems unexpected since only a few drawables are successfuly retrieved. And what's ever stranger is that it happens only in S4 and not in my old xperia mini with much less memmory available – Anonymous Mar 08 '15 at 23:37
  • And just in case I haven't reached your "strange" standars I get the same Failure getting entry error at onCreateContextMenu on a listview item long click. Even though those string resources are loaded properly. AGAIN ONLY IN S4 – Anonymous Mar 08 '15 at 23:45

0 Answers0