Android - 从三星Galaxy S4中获取其他应用程序的资源可用性崩溃

时间:2015-03-08 23:22:10

标签: android resources out-of-memory samsung-mobile

在三星Galaxy S4中,我遇到了一个奇怪的问题。 我已编写此代码以从其他应用程序获取所有资源drawable。它在模拟器和我测试过的其他两个设备中工作正常,但在S4中,在抛出此消息的许多异常后抛出内存不足错误:

ResourceType: Failure getting entry for 0x7f020292 (t=1 e=658) in package 0 (error -75)

它成功获取了一些资源,在其他资源上失败并最终崩溃。我不明白为什么发生内存不足错误,因为drawables没有真正加载。有没有办法阻止这种情况发生?

此外......所有应用程序都不会发生这种情况。这有点随意。

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) {  } 
}

0 个答案:

没有答案