ANDROID ACTION_VIEW 显示来自 URI 的错误图像
我有一个 Android 应用程序,允许用户选择一张已经拍摄的照片。用户选择照片后,我会获取该照片的 URI 信息,使用该信息创建位图,然后在 ImageView 中设置位图。这一切都工作得很好。
然后,我为用户提供选择要查看的图片的选项。当用户选择查看图片时,我启动 ACTION_VIEW 意图,传递 URI 数据。
Intent intent = new Intent(Intent.ACTION_VIEW, pictureUri);
startActivity(intent);
正如您所看到的,这是非常简单的代码,没有什么特别的事情发生。问题是,当我在运行 Android 2.1 的 Droid 上运行此代码时,大约十分之六或七次,应用程序将显示正确的图片。但另外三四次,我看到的图片是错误的。而且,每次显示错误的图片时,总是显示相同的错误图片。事实上,我在大多数情况下都能看到正确的图片,这让我相信我在代码中所做的一切都很好,所以有人可以告诉我您以前是否见过这个问题,更好的是,有解决方案吗?
以下是我运行此命令时在 Droid 上观察到的确切序列(注意:在启动 Activity 之前图像 URI 已保存):
- 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
请记住,当我使用已保存的 URI(从数据库检索)启动 Activity 时,我根据 onCreate() 中的 URI 数据设置了一个 ImageView,并且 ImageView 中显示的图像始终是正确的图像。直到我真正决定使用 ACTION_VIEW 查看图像时,我才看到奇怪的行为。我知道这不是这两张照片的具体内容。我过去使用其他照片观察过这种行为,并得到了相同的行为。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我会在用户最初选择
pictureUri
时以及将其推入ACTION_VIEW
意图之前调试/记录pictureUri
的值,看看它是否是你期望它在那些地方是什么样子。听起来好像在用户选择图片后,您将 uri 存储在本地,并再次抓取它以启动意图。因此,也许您将如果没有更多代码(如何保存和恢复 uri),很难说清楚,但这就是我开始寻找的地方。
First, I'd debug/log the value of
pictureUri
both when it is originally selected by the user and before you shove it into yourACTION_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'reWithout more code (how you save and restore uris) it's hard to tell, but that's where I'd start looking.