检索 Picasa 图像以从图库上传
我正在开展一项活动和相关任务,允许用户从图库中选择一张图像用作其个人资料图片。一旦做出选择,图像就会通过其 API 上传到网络服务器。我有来自画廊的正常工作图像。但是,如果所选图像来自 Picasa 网络相册,则不会返回任何内容。
我做了很多调试并将问题缩小到这种方法。
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
//cursor is null for picasa images
if(cursor!=null)
{
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
else return null;
}
Picasa 图像返回空光标。然而,MediaStore.Images.Media.DATA 对于它们来说不为空。它只返回一个#id,所以我猜测该地址没有实际的位图数据。 Picasa 图像是否存储在设备本地?
我还从文档中注意到 MediaStore.Images.ImageColumns.PICASA_ID 存在。此值适用于选定的 picasa 图片,但不适用于其他图库图片。我想如果图像没有存储在本地,我可以使用这个值来获取图像的 URL,但我在任何地方都找不到有关它的任何信息。
I am working on an activity and associated tasks that allow users to select an image to use as their profile picture from the Gallery. Once the selection is made the image is uploaded to a web server via its API. I have this working regular images from the gallery. However, if the image selected is from a Picasa Web Album nothing is returned.
I have done a lot of debugging and narrowed the problem down to this method.
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
//cursor is null for picasa images
if(cursor!=null)
{
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
else return null;
}
Picasa images return a null cursor. MediaStore.Images.Media.DATA is not null for them, however. It only returns an #id, so I am guessing that there is no actual bitmap data at the address. Are Picasa images stored locally on the device at all?
I also noticed from the documentation that MediaStore.Images.ImageColumns.PICASA_ID exists. This value exists for selected picasa images but not other gallery images. I was thinking I could use this value to get a URL for the image if it is not store locally but I can not find any information about it anywhere.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了完全相同的问题,
最后我找到的解决方案是启动 ACTION_GET_CONTENT 意图而不是 ACTION_PICK,然后确保为临时文件提供带有 uri 的 MediaStore.EXTRA_OUTPUT 额外内容。
这是启动意图的代码:
您可能需要 mTempFile.createFile()
然后在 onActivityResult 中,您将能够以这种方式获取图像
希望这有帮助
然后您应该在完成时删除临时文件(它位于内部存储中)照原样,但你可以使用外部存储,我想这会更好)。
I have faced the exact same problem,
Finally the solution I found, was to launch an ACTION_GET_CONTENT intent instead of an ACTION_PICK, then make sure you provide a MediaStore.EXTRA_OUTPUT extra with an uri to a temporary file.
Here is the code to start the intent :
You might need to mTempFile.createFile()
Then in onActivityResult, you will be able to get the image this way
Hope this helps
Then you should delete your temporary file on finish (it is in internal storage as is, but you can use external storage, I guess it would be better).
为什么使用
managementQuery()
方法?该方法已被弃用。如果您想将
Uri
转换为Bitmap
对象,请尝试以下代码:Why are you using the
managedQuery()
method? That method is deprecated.If you want to convert a
Uri
to aBitmap
object try this code: