13

我正在开展一项活动和相关任务,允许用户从图库中选择一张图片作为他们的个人资料图片。一旦做出选择,图像就会通过其 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对他们来说不是空的。它只返回一个#id,所以我猜测该地址没有实际的位图数据。Picasa 图像是否完全存储在设备本地?

我还从文档中注意到MediaStore.Images.ImageColumns.PICASA_ID存在。此值适用于选定的 picasa 图片,但不适用于其他图库图片。我在想我可以使用这个值来获取图像的 URL,如果它没有存储在本地,但我在任何地方都找不到关于它的任何信息。

4

2 回答 2

5

我遇到了完全相同的问题,
最后我找到的解决方案是启动 ACTION_GET_CONTENT 意图而不是 ACTION_PICK,然后确保为临时文件提供带有 uri 的 MediaStore.EXTRA_OUTPUT 额外内容。这是启动意图的代码:

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
                                    }
}

希望这可以帮助

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).

于 2011-05-12T12:43:12.420 回答
1

Why are you using the managedQuery() method? That method is deprecated.

If you want to convert a Uri to a Bitmap object try this code:

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;
}
于 2012-10-11T08:12:07.047 回答