Android 检索Picasa图像以便从库上载
我正在进行一项活动和相关任务,允许用户从图库中选择一幅图像作为其个人资料图片。一旦做出选择,图像将通过其API上传到web服务器。我有这张画廊的正常图片。但是,如果选择的图像来自Picasa网络相册,则不会返回任何内容 我已经做了很多调试,并将问题缩小到这种方法Android 检索Picasa图像以便从库上载,android,picasa,Android,Picasa,我正在进行一项活动和相关任务,允许用户从图库中选择一幅图像作为其个人资料图片。一旦做出选择,图像将通过其API上传到web服务器。我有这张画廊的正常图片。但是,如果选择的图像来自Picasa网络相册,则不会返回任何内容 我已经做了很多调试,并将问题缩小到这种方法 public String getPath(Uri uri) { String[] projection = { MediaStore.Images.Media.DATA }; Cursor cursor = manag
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\u GET\u CONTENT intent而不是ACTION\u PICK,然后确保为临时文件提供一个MediaStore.EXTRA\u OUTPUT EXTRA和一个uri。 以下是启动意图的代码:
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转换为位图对象,请尝试以下代码:
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;
}
file=mTempFile行到底在做什么?文件来自哪里?位图就是在这里存储的吗?它是通过以下命令启动的:mTempFile=getfilestreampathyurtempfile;我了解MTEMP文件的来源。变量文件来自哪里?我在任何地方都看不到设置;将在应用程序目录/data/app/package/files/yourTempFile中创建文件名“yourTempFile”。如果愿意,可以使用其他路径初始化它。这只是获取文件的一种简单方法。唯一重要的是提供一个符合您意图的uri,以便在选择picasa图片时填充文件。但是,Picasa图像在被选中后将存储在库中的下载中。我尝试在获得所需位图后调用mTempFile.delete,但它们仍然被保存。以下是dsk 8-13的完整、更准确的工作代码:是的,这对我很有用。。当我们从intent获取uri时,它会将文件保存在位图中,无论它是来自设备库还是来自picasa图像,在本例中,picasa图像发送给我们content://com.google.android.gallery3d.