Android MediaStore 将照片名称设置为 NULL
我有一个应用程序,允许用户使用 Intent ACTION_IMAGE_CAPTURE 拍摄并保存新照片。用户接受新拍摄的图片后,我创建一个 ContentValue 对象来设置图片信息,然后将图片插入 MediaStore 并发送广播,以便用户在打开图库等图片查看器应用时可以看到照片:
ContentValues newImage = new ContentValues(3);
newImage.put(Media.DISPLAY_NAME, mPicName);
newImage.put(Media.MIME_TYPE, "image/png");
newImage.put(MediaStore.Images.Media.DATA, path);
mPictureUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, newImage);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mPictureUri));
所有这些代码都可以正常工作。照片已正确保存,我可以使用图库查看照片。问题是,当我在图库中查看照片并选择“详细信息”时,它显示照片名称为“空”。是的,我知道上面的变量 mPicName 不为空并且始终具有正确的值。
奇怪的是,当我在运行 Android 2.1 的 Droid 上运行我的应用程序时,当我选择将文件复制到手机和计算机上/从手机和计算机上复制文件时,我会在 Windows 计算机上打开 Droid,当我进入应用程序的创建时文件夹中的照片并查看新照片,名称正确。然后,当我禁用从手机/计算机复制文件并返回图库以查看 Droid 上的文件时,名称突然正确并且不再为空。
有人可以告诉我为什么我的代码运行后名称为空,以及为什么在我的计算机而不是手机上查看文件后名称是正确的?有没有办法在保存图片时使名称非空?我敢打赌,如果我关闭手机然后重新打开,该名称将是正确的,但我不确定如何让事情立即生效。
I have an app that allows the user to take and save a new picture using the Intent ACTION_IMAGE_CAPTURE. After the user accepts the newly taken picture, I create a ContentValue object to set the picture information, and then I insert the picture into the MediaStore and send a broadcast so that the user can see the photo when opening a picture viewer app such as gallery:
ContentValues newImage = new ContentValues(3);
newImage.put(Media.DISPLAY_NAME, mPicName);
newImage.put(Media.MIME_TYPE, "image/png");
newImage.put(MediaStore.Images.Media.DATA, path);
mPictureUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, newImage);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mPictureUri));
All of this code works fine. The photo is correctly saved, and I can view the photo using gallery. The problem is that when I view the photo in gallery and select "Details" it shows the photo name as "null." And yes, I know for a fact that the variable mPicName above is not null and always has the correct value.
The odd thing is that, when running my app on my Droid running Android 2.1, when I choose to copy files to/from the phone and my computer, I open up the Droid on my Windows computer, and when I go into my app's created folder for photos and view the new photo, the name is correct. Then, when I disable copying files from the phone/computer and go back into gallery to view the file on my Droid, the name is suddenly correct and is no longer null.
Can someone tell me why the name is null after my code runs, and why the name is correct after viewing the file on my computer rather than on the phone? Is there a way to get the name non-null right when the picture is saved? I'd bet that the name would be correct if I turned my phone off and turned it back on, but I'm not sure how exactly to get force things to work immediately.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案是查看 MediaStore 的来源。
一个地方在这里:
http://devdaily.com/java/jwarehouse /android/core/java/android/provider/MediaStore.java.shtml
基本上他们还添加了图像的缩略图表示,而不是
他们使用 Media.DISPLAY_NAME Media.TITLE。
我使用了稍微修改过的代码并且工作得很好。
Answer is by looking at the sources of MediaStore.
One places is here:
http://devdaily.com/java/jwarehouse/android/core/java/android/provider/MediaStore.java.shtml
Basically they also add a thumbnail representation of the image and instead of
Media.DISPLAY_NAME they use Media.TITLE.
I used that code adapted a little and worked fine.