4

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.

Qlimax
  • 5,241
  • 4
  • 28
  • 31
  • ok, as nobody reply, I decided to go forward, and take the approach everything-is-an-uri. So now instead of managing ints inside the product class I deal with Uri However,I'm having problems when doing something like this (sample code): Uri uri=Uri.parse("android.resource://my.package.name/"+R.drawable.image); imgView.setImageURI(uri); The image just display blank... and on the logcat I can see 02-22 17:00:19.352: INFO/System.out(1717): resolveUri failed on bad bitmap uri: android.resource://my.package.here/2130837520 any idea on what I'm doing wrong? thank you – Qlimax Feb 22 '10 at 17:19
  • You seem to be on the right direction and would have expected the code to work. You could also try the format uri=Uri.parse("android.resource://my.package.name/drawable/image"); If you get stuck, see if the Resources class might be able to help you http://developer.android.com/reference/android/content/res/Resources.html – dparnas Feb 22 '10 at 22:59
  • The uri=Uri.parse("android.resource://my.package.name/drawable/image"); produce the same behaviour... Looking at the Resource class, I can't find anything useful for my purpose... I'm frustrated, these are things that must WORK, a developer shouldn't lose 2 day to solve this kind of problems, DAMN!!! the documentation on this topic is embarassing...(pretty much nothing) – Qlimax Feb 23 '10 at 19:17

1 Answers1

1

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
  • 10,658
  • 6
  • 43
  • 60