开发者

Passing an ID from startActivityForResult to onActivityResult

I have a ListView with a ContextMenu entry, that starts the users photo gallery开发者_运维知识库 with startActivityForResult so he can pick an image to associate it with the ListItem. Picking the image works pretty well, and i get the image uri from the Intent passed to onActivityResult.

The problem is, i don't get the ListItem's id in onActivityResult. I tried to attach it to the intent that i pass to startActivityForResult via putExtra, but the intent that i receive in onActivityResult is obviously not the same anymore.

Is there some way to pass this information to onActivityResult? Here's my code:

private void pickPhoto(long itemId) {
    Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    i.putExtra(ITEM_ID, itemId);
    startActivityForResult(i, PICK_PHOTO_REQUEST);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case PICK_PHOTO_REQUEST:

        long itemId = data.getLongExtra(ITEM_ID, 0);
        long imageId = ContentUris.parseId(data.getData());

        // That here says itemId is 0:
        Log.d(TAG, "adding image " + imageId + " to item " + itemId);

        setPhoto(itemId, imageId);
        break;

    default:
        super.onActivityResult(requestCode, resultCode, data);
    }
}

Thanxalot!


Ok, I stored the id in a field variable now and that works. Though I'm not so happy about this solution, because it would fail if the user starts two requests in parallel. More sort of a hack.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜