Issue
Context
Each item in a listview loads an image. Given the item's data the id for the resource(drawable) is returned. This id is used in the imageview's setResourceId.
Problem
When the apk is signed, nothing is displayed ( however no error occurs).
Testing
Ensuring the correct id for the resource is loaded. One item was created whose matching resource is R.drawable.banana, the id returned by the function was logged as well as the value R.drawable.banana (shown below).
int id = CropDataHelper.getCropsDrawable(myContext, txt);
Log.i("image id:"+id,"found id Fragment View Cycles");
Log.i("raw draw:"+R.drawable.banana,"drawable Fragment View Cycles");
The values logged were the same hence the correct id was being returned.
Further Testing Reveals
If using the resource directly, R.drawable.example the image is displayed, even though the function returns the same value.
id = R.drawable.banana;
...
iv.setImageResource(id);
Previous Failed attempts
- Putting resource into a different folders (no-dpi, etc)
folder attempt reference. However since the image does show if I use the direct R reference this was a baseless attempt.
- Using the drawable resource
Use the id attained to load the drawable directly and set it.
Drawable d = ContextCompat.getDrawable(myContext,id);
...
iv.setImageDrawable(d);
Actual Code Snippet
int id = CropDataHelper.getCropsDrawable(myContext, txt);
Log.i("image id:"+id,"found id Fragment View Cycles");
Log.i("raw draw:"+R.drawable.banana,"drawable Fragment View Cycles");
// id = R.drawable.banana; - testing use of direct resource
// Drawable d = ContextCompat.getDrawable(myContext,id); drawable attempt
ImageView iv = ((ImageView) row.findViewById(R.id.icon_purchaseType));
if (id != -1) {
iv.setImageResource(id);
// iv.setImageDrawable(d); - drawable attempt
}
Final Remarks
The problem does not occur while debugging, only on a signed apk, this leads me to believe there is some underlying permissions that R.drawable.example unlocks on a signed apk but when trying to use the id directly these permissions are not unlocked. Any help is appreciated, thank you.
Solution
Work Around
Hashmap of string names to R's ids
Reasoning
It seems there may have been some permission not available when trying to read the files (Only occurs in signed - even though these are app images). To work around this, instead of using a json file to load image files by name, A hashmap was created with strings to match R's ids.
Sample Code
//create hashmap
private HashMap<String,Integer> resources;
//fill hash map with resources
resources.put("FIRST",R.drawable.example_img_1);
resources.put("SECOND",R.drawable.example_img_2);
//simple method to access resources
public Integer getResourceId(String key){
if(resources.containsKey(key))
return resources.get(key);
return R.drawable.default_image;
}
Answered By - steff_bdh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.