0

I have an object to store image id.

public class MyObject {
    private Integer imageId;

    public MyObject(Integer imageId) {
        this.imageId = imageId;
    }

    public Integer getImageId() {
        return imageId;
    }

}

I will use this to set to imageview

imageView.setImageResource(myObject.getImageId());

But sometimes the image is not from res folder

PackageManager pm;
pm = getPackageManager();
pm.getApplicationIcon("packagname");

But I cannot store it because it returns drawable, not int

How can I store any source image in android?

CL So
  • 3,647
  • 10
  • 51
  • 95
  • 1
    Have you tried using `getDrawable(id, theme)` to load your resource drawable, and then storing the resulting drawable (either from the pm or from the resource) in your object? – Bert Peters Jun 28 '15 at 18:05

2 Answers2

0

If your image resource is called example.png and it is in one of your drawable folders (in res), it's ID can be retrieved by:

int resId = R.drawable.example;
poisondminds
  • 427
  • 4
  • 8
0

You can check that if drawable is null then use imageId & pick image from resources folder. If return object is instance of drawable then save it in drawable variable else instance of Integer save in your imageId variable.

Check this public class MyObject {

private Integer imageId;

private Drawable myDrawable;

    public MyObject(final Integer imageId, final Drawable myDrawable) {
        this.imageId = imageId;
        this.myDrawable = myDrawable;
    }

public Integer getImageId() {
    return imageId;
}

public Integer getMyDrawable() {
    return myDrawable;
}

}

In your Activity:-

if(myObject.getMyDrawable()!= null){
imageView.setImageDrawable(myObject.getMyDrawable());
}
else {
imageView.setImageResource(myObject.getImageId());
} 
Rahul
  • 10,457
  • 4
  • 35
  • 55
  • finally I found the solution from [link](http://stackoverflow.com/questions/7815689/how-do-you-obtain-a-drawable-object-from-a-resource-id-in-android-package) – CL So Jun 29 '15 at 05:05