【问题标题】:Intent extras are null when picking an image from gallery从图库中选择图像时,Intent extras 为空
【发布时间】:2013-09-13 21:03:24
【问题描述】:

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

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

我在启动活动 (startActivityForResult) 时传递了一些意图附加信息,但所有意向附加信息均为空。

代码sn-ps(以防万一):

这是我的 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();  
        }
    }       

我正在使用 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);

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

我们将不胜感激任何想法和建议。想了很多。

【问题讨论】:

标签: android android-intent


【解决方案1】:

实际上我想通了.. 当您向 MediaStore 或相机等系统活动发送意图时...... onActivityResult 不会有您在调用活动时发送的意图额外内容。

这可能是设计使然,仅包含系统活动提供的附加功能。例如,从图库中选择图像后,从图库返回的意图将仅包含包含图像路径的 URI。

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

【讨论】:

  • 是正确的(我发现这很困难!)。相反,我编写了两种方法……一种保存我想在 SharedPrefrences 中传递的任何变量,另一种检索和删除它们。效果非常好。
猜你喜欢
  • 2019-11-27
  • 1970-01-01
  • 1970-01-01
  • 2016-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-29
  • 1970-01-01
相关资源
最近更新 更多