[英]Retrieve Picasa Image for Upload from Gallery
我正在开展一项活动和相关任务,允许用户从图库中选择要用作其个人资料图片的图像。 选择完成后,图像将通过其API上载到Web服务器。 我有来自画廊的常规图像。 但是,如果所选图像来自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对于它们不是 null。 它只返回一个#id,所以我猜测地址上没有实际的位图数据。 Picasa图片是否本地存储在设备上?
我还从文档中注意到MediaStore.Images.ImageColumns.PICASA_ID存在。 此值适用于所选的picasa图像,但不适用于其他图库图像。 我想我可以使用此值来获取图像的URL,如果它不是本地存储但我无法在任何地方找到任何相关信息。
我遇到了同样的问题,
最后我发现的解决方案是启动ACTION_GET_CONTENT意图而不是ACTION_PICK,然后确保向临时文件提供带有uri的MediaStore.EXTRA_OUTPUT。 以下是启动intent的代码:
public class YourActivity extends Activity {
File mTempFile;
int REQUEST_CODE_CHOOSE_PICTURE = 1;
(...)
public showImagePicker() {
mTempFile = getFileStreamPath("yourTempFile");
mTempFile.getParentFile().mkdirs();
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTempFile));
intent.putExtra("outputFormat",Bitmap.CompressFormat.PNG.name());
startActivityForResult(intent,REQUEST_CODE_CHOOSE_PICTURE);
}
(...)
}
您可能需要mTempFile.createFile()
然后在onActivityResult中,您将能够以这种方式获取图像
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
case REQUEST_CODE_CHOOSE_PICTURE:
Uri imageUri = data.getData();
if (imageUri == null || imageUri.toString().length() == 0) {
imageUri = Uri.fromFile(mTempFile);
file = mTempFile;
}
if (file == null) {
//use your current method here, for compatibility as some other picture chooser might not handle extra_output
}
}
希望这可以帮助
然后你应该在完成后删除你的临时文件(它在内部存储中,但你可以使用外部存储,我想它会更好)。
为什么使用managedQuery()
方法? 该方法已弃用。
如果要将Uri
转换为Bitmap
对象,请尝试以下代码:
public Bitmap getBitmap(Uri uri) {
Bitmap orgImage = null;
try {
orgImage = BitmapFactory.decodeStream(getApplicationContext().getContentResolver().openInputStream(uri));
} catch (FileNotFoundException e) {
// do something if you want
}
return orgImage;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.