从图库中选择图像时,额外的意图为空

Gur*_*uru 3 android android-intent

我一直在研究这个,但我无法找到答案。

我正在使用媒体存储意图从图库中选择图像,并且能够在 onActivityResult 方法中获取图像文件路径。(我知道如何在意图和文件路径中获取 URI)。

我在启动活动时传入了一些额外的意图 (startActivityForResult),但所有的额外意图都是空的。

代码片段(以防万一):

这是我的 onActivityResult 代码,它正在工作,我得到了图像路径

/* On activity result from image button */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        System.out.println("Result Code" + resultCode);
        if(requestCode == FavoriteListAdapter.IMAGE_PICK_CODE && data != null && data.getData() != null && resultCode == FragmentActivity.RESULT_OK) {
            Uri _uri = data.getData();

            //User had pick an image.
            Cursor cursor = getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
            cursor.moveToFirst();

            //Link to the image
            String imageFilePath = cursor.getString(0);


            System.out.println("imagefilepath" + imageFilePath);  
            System.out.println(data.getStringExtra("exp"));
            cursor.close();  
        }
    }       
Run Code Online (Sandbox Code Playgroud)

我用 startActivityForResult 开始我的活动

Intent imageIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            imageIntent.setType("image/*");
            imageIntent.putExtra("exp", "testing");

            ((FragmentActivity)view.getContext()).startActivityForResult(imageIntent, IMAGE_PICK_CODE);
Run Code Online (Sandbox Code Playgroud)

我应该能够在 onActivityForResult 中获得字符串“testing”,但我得到的只是空值。

任何想法和建议将不胜感激。THnkas 很多。

Gur*_*uru 5

实际上我想通了.. 当您向 MediaStore 或相机等系统活动发送意图时…… onActivityResult 将不会有您在调用活动时发送的意图附加信息。

这可能是设计使然,并且只包含系统活动提供的额外内容。例如,从图库中选择图像后,图库的返回意图将仅包含包含图像路径的 URI。

相机或任何系统活动也是如此。

  • 是正确的(我发现这一点很困难!)。相反,我编写了两种方法......一种保存我想在 SharedPrefences 中传递的任何变量,另一种检索和删除它们。它工作得非常好。 (2认同)