首页 > 解决方案 > 拍照时捆绑键返回 null

问题描述

我正在使用 ro 启动从相机捕获图像的意图:

public void takePhoto(View view) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
         try {

             Bundle bundle = new Bundle();
             f = createImageFile(); // creates a new image file into which I want to store the photo
             bundle.putString( "currentPhotoPath", f.getAbsolutePath() );
             intent.putExtras(bundle);
             startActivityForResult(intent, CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE);

        } 
        catch (IOException e) {
            e.printStackTrace();
        }        
    }

crates 一个空的f.createImageFileImage 对象,我想在其中存储从相机拍摄的照片。我试图将这个图像对象的路径传递给捆绑包,如下所示:

bundle.putString( "currentPhotoPath", f.getAbsolutePath() );

然后我使用这段代码得到结果:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

            super.onActivityResult(requestCode, resultCode, data);
            if(requestCode == CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE ) {
                Bundle bundle = data.getExtras();
                String currentPhotoPath = bundle.getString("currentPhotoPath");
                File takenPhoto = new File("currentPhotoPath");

                if (resultCode == RESULT_OK) {
                   if(takenPhoto.length() <= 0) {
                       takenPhoto.delete();
                   }
                   this.checkForUploads();
                }
                else if(resultCode == RESULT_CANCELED) {
                    takenPhoto.delete();
                } 
            }       
        }

我期待在 data.getExtras()保存新图像对象路径的键“currentPhotoPath”中找到,但它始终为空。相反,我得到的唯一关键是我认为它包含实际位图数据的数据。我在 so 上看到过类似的问题,我认为我的做法是正确的。

标签: android

解决方案


我在 so 上看到过类似的问题,我认为我的做法是正确的。

对不起,不行。

您投入的额外Intent内容Intent。在您的情况下,它会进行一些具有ACTION_IMAGE_CAPTURE <intent-filter>. 该代码如何处理额外内容取决于该活动的开发人员。在您的情况下,由于您自己发明了一个新Intent的附加功能,因此您的附加功能将被忽略,因为ACTION_IMAGE_CAPTURE活动的开发人员不会知道要寻找它。

Intent交付对象与您使用的onActivityResult()对象完全不同。特别是,您添加的额外内容不需要包含在其他应用程序的响应中。IntentstartActivityForResult()startActivityForResult() IntentIntent


推荐阅读