tags:

views:

126

answers:

1

I have an Android application that allows the user to select a photo that has already been taken. Once the user selects a photo, I grab the URI information for that photo, use that information to create a bitmap, and then I set the bitmap in an ImageView. This all works perfectly fine.

I then give the user the option to select the picture for viewing. When the user chooses to view the picture, I launch an ACTION_VIEW intent, passing the URI data.

Intent intent = new Intent(Intent.ACTION_VIEW, pictureUri);
startActivity(intent);

As you can see, this is very simple code, nothing special going on. The problem is that, when I run this code on my Droid running Android 2.1, about 6 or 7 times out of 10, the application will display the correct picture. But the other 3 or 4 times, I get shown the wrong picture. Also, each time the wrong picture is shown, its always the same incorrect picture being shown. The fact that I see the right picture the majority of the time leads me to believe everything I'm doing in code is fine, so can anyone tell me if you have seen this problem before, and better yet, is there a solution?

Here is the exact sequence that I observe on my Droid when I run this (Note: Image URI is already saved before I start the Activity):

- I choose "View Photo" in the Activity
- When things work, I get taken to the gallery and shown image 74
- Each time things do not work, I get shown image 82

Keep in mind that when I start the Activity with the URI already saved (retrieved from database), I set an ImageView based on the URI data in onCreate(), and the Image being shown in the ImageView is ALWAYS the correct image. It's not until I actually decide to view the image using ACTION_VIEW that I see odd behavior. And I know it's not something specific about these 2 photos. I observed this behavior using other photos in the past, and got the same behavior.

A: 

First, I'd debug/log the value of pictureUri both when it is originally selected by the user and before you shove it into your ACTION_VIEW intent, to see if it is what you expect it to be in those places. It sounds like after the user chooses a pic, you're storing the uri locally, and grabbing it again in order to start the intent. So maybe you're

  • storing a random incorrect uri in the correct place
  • storing the correct uri in an incorrect place
  • grabbing the "saved" uri from an incorrect place.

Without more code (how you save and restore uris) it's hard to tell, but that's where I'd start looking.

Josh
Hi Josh, I think the URI data was fine. In onCreate, I opened my database where the URI data was stored, and I set the picture based on that data. So, when I would switch between portrait and landscape, the Activity would be destroyed and onCreate called again, grabbing the data from the database.In any case, after I posted my question, I did some code clean up, really just going through and plugging anything that was a potential memory leak, and suddenly I'm not seeing the problem any longer. I'm not sure what to make of this, but the problem is no longer present.
Michael
Awesome, glad to hear it's not a problem any longer.
Josh
Yeah me too! Thanks again for the suggestions.
Michael