tags:

views:

321

answers:

1

Hi everyone, I have the following problem:

suppose that until now, I am using R.drawable.img to set the image in some imageviews with

imgView.setImage(aProduct.getImgId());

the simplified product class look like this:

class Product{
        int imgId;

        ....
        public void setImgId(int id){
        this.imgId=id;
        }

        public int getImgId(){
        return this.imgId
        }

        ...




    }

my application is now "evolved" because the user can add customized products taking the img from the camera and getting the Uri of the picture. and to set the image on the ImageView imgView.setImgURI(Uri)

Now my question is: what would be the best approach to have a mixed int/Uri image resources ambient? can I obtain the Uri of a "R.drawable.img"?

I'm not sure if my question is clear, I mean:

I have to check, before to set the imageview, if my product has an Uri or an int Id, and then make an "if" to call the appropriate method, or there is a simpler solution?

Thank you for reading, and sorry for my english.

Regards.

A: 

Your problem is that there are basically 3 types of image resources:

  • R.id... resources: internal resources, such as icons you put into the res folder
  • content URI's: local files or content provider resources such as content:// or file:///sdcard/...
  • remote file URL's: images on the web, such as http://...

You are looking for a way to pass around one identifier that can deal with all three. My solution was to pass around a string: either the toString() of the URI's, or just the string respresentation of the R.id integer.

I'm using the following code, for example, to deal with this and make the code work with it:

public static FastBitmapDrawable returnAndCacheCover(String cover, ImageRepresentedProductArtworkCreator artworkCreator) {
    Bitmap bitmap = null;
    Uri coverUri = null;
    boolean mightBeUri = false;
    //Might be a resId. Needs to be cached. //TODO: problem: resId of default cover may not survive across versions of the app.
    try {
        bitmap = BitmapFactory.decodeResource(Collectionista.getInstance().getResources(), Integer.parseInt(cover));
    } catch (NumberFormatException e) {
        //Not a resId after all.
        mightBeUri=true;
    }
    if(bitmap==null || mightBeUri){
        //Is not a resId. Might be a contentUri.
        try {
            coverUri = Uri.parse(cover);
        } catch (NullPointerException ne) {
            //Is null
            return null;
        }
    }
    if(coverUri!=null){
        if(coverUri.getScheme().equals("content")){
            //A contentUri. Needs to be cached.
            try {
                bitmap = MediaStore.Images.Media.getBitmap(Collectionista.getInstance().getContentResolver(), coverUri);
            } catch (FileNotFoundException e) {
                return null;
            } catch (IOException e) {
                return null;
            }
        }else{
            //Might be a web uri. Needs to be cached.
            bitmap = loadCoverFromWeb(cover);

        }
    }
    return new FastBitmapDrawable(bitmap);
}

You might be interested to take over the logic part. Ofcourse cover is the string in question here.

Forget android.resource:// as a replacement for the R.id... integer. Claims are going round it does not work: http://groups.google.com/group/android-developers/browse_thread/thread/a672d71dd7df4b2d

To find out how to implement loadCoverFromWeb, have a look around in other questions or ping me. This web stuff is kind of a field of it's own.

(Based on GPLv3 code out of my app Collectionista: https://code.launchpad.net/~pjv/collectionista/trunk)

pjv