我正在开展一项活动和相关任务,允许用户从图库中选择要用作其个人资料图片的图像.选择完成后,图像将通过其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;
}
Run Code Online (Sandbox Code Playgroud)
Picasa图片会返回一个空游标. MediaStore.Images.Media.DATA是不是空了他们,但是.它只返回一个#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);
}
(...)
}
Run Code Online (Sandbox Code Playgroud)
您可能需要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
}
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助
然后你应该在完成后删除你的临时文件(它在内部存储中,但你可以使用外部存储,我想它会更好).
| 归档时间: |
|
| 查看次数: |
7232 次 |
| 最近记录: |