【问题标题】:onActivityResult returns null data for an Image CaptureonActivityResult 返回图像捕获的空数据
【发布时间】:2013-07-23 13:00:54
【问题描述】:
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    filePath = getOutputMediaFile(FileColumns.MEDIA_TYPE_IMAGE);
    File file = new File(filePath);
    Uri output = Uri.fromFile(file);
    Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    i.putExtra(MediaStore.EXTRA_OUTPUT, output);
    startActivityForResult(i, RETURN_FILE_PATH);
}

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    //data is always null here.
    //requestCode = RETURN_FILE_PATH;
    //resultCode = Activity.RESULT_OK;
}

我检查了 fileoutput Uri 的值,两者都很好,并且捕获的 图像实际上存在于该位置

但是onActivityResult中返回的数据即使在捕获图像之后也始终是null

编辑:

我检查了这个问题:

onActivityResult returns with data = null

上面写着:

每当您通过带有相机意图的 EXTRAOUTPUT 来保存图像时 onActivityResult 中的 data 参数总是返回 null。所以, 而不是使用数据来检索图像,而是使用文件路径来 检索位图。

也许该解决方案对我有用。但到目前为止,我的上述代码对于相同的场景来说是一个有效的代码。

【问题讨论】:

    标签: android onactivityresult


    【解决方案1】:

    根据post,当您预先插入 uri 时,数据为空。这意味着您已经在此处定义了输出 uri:

      i.putExtra(MediaStore.EXTRA_OUTPUT, output);
    

    所以当你得到一个 Activity.RESULT_OK;只需通过已知 url 加载拍摄的照片。

    【讨论】:

    • 是的,刚刚看到那个帖子。但是相同的代码在过去的 6 个月里一直有效,为什么突然之间它的行为方式是这样的:(
    • 不幸的是,我不是这个主题的专家,我猜它是与硬件相关的设备特定功能。这篇文章提到三星设备上存在不同的行为:stackoverflow.com/q/8248327/1965084
    【解决方案2】:

    试试这个对我有用的代码。

    else if(requestCode == Constant.PICK_FROM_CAMERA)
                {
    
                    if (resultCode == Activity.RESULT_OK) 
                    {
                        if(data!=null)
                        {
                            mImageCaptureUri = data.getData();
                            //path= mImageCaptureUri.getPath();
                            try
                            {
                                path = getPath(mImageCaptureUri,Wonderlistpage.this); //from Gallery
                            }
                            catch(Exception e)
                            {
                                path = mImageCaptureUri.getPath();
                                Log.i("check image attach or not", e.toString());
                            }
    
                            String arr[] = path.split("/");
                            int i;
                            String k = null;
                            for(i=0;i<arr.length;i++)
                            {
                                k=arr[i];       
                            }
                            photoname="_"+String.valueOf(System.currentTimeMillis()) +k;
                             if(setprofileimage_sendimagewithmessage==1)
                             {
                                 performCrop(mImageCaptureUri);
                             }
                             else
                             {
                                  loading_details="CAMERA";
                                  new performBackgroundTask33().execute();
                             }
                        }
                        else
                        {
    file1  = new File(Environment.getExternalStorageDirectory(),
                                        String.valueOf(System.currentTimeMillis()) + "_FromCamera.jpg");
    
                            Uri mImageCaptureUri = Uri.fromFile(file1);
                            try
                            {
                                path = getPath(mImageCaptureUri,Wonderlistpage.this); //from Gallery
                            }
                            catch(Exception e)
                            {
                                path = mImageCaptureUri.getPath();
                                Log.i("check image attach or not", e.toString());
                            }
                            String arr[] = path.split("/");
                            int i;
                            String k = null;
                            for(i=0;i<arr.length;i++)
                            {
                                k=arr[i];       
                            }
                            photoname="_"+String.valueOf(System.currentTimeMillis()) +k;
                             if(setprofileimage_sendimagewithmessage==1)
                             {
                                 performCrop(mImageCaptureUri);
                             }
                             else
                             {
                                  loading_details="CAMERA";
                                  new performBackgroundTask33().execute();
                             }
    
                        }
    
                        //new UploadTask().execute();
                    }
                }
    

    【讨论】:

      【解决方案3】:

      试试下面的代码

              {
                  final String[] imageColumns = { MediaStore.Images.Media._ID,MediaStore.Images.Media.DATA };
      
                  final String imageOrderBy = MediaStore.Images.Media._ID + " DESC";
                  Cursor imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns, null, null, imageOrderBy);
                  imageCursor.moveToFirst();
                  do {
                      String fullPath = imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
                      if (fullPath.contains("DCIM")) {
      
                          //get bitmap from fullpath here.
                          return;
                      }
                  }
                  while (imageCursor.moveToNext());
      

      【讨论】:

        【解决方案4】:

        只需将此代码放入您的 onActivityResult。我在某些设备上遇到了同样的问题,这解决了我的问题。希望这也能帮助你。

        try {
        
            Uri selectedImage = output;
        
            if (selectedImage == null)
                return;
        
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
        
        } catch (Exception e) {
            return;
        }     
        

        您将在 picturePath 变量中获得图片路径,在 selectedImage 变量中获得 Uri。

        【讨论】:

          【解决方案5】:

          如果您的活动在清单中将启动模式设置为 singleInstance,那么您将面临这个问题。尝试改变它。因为它每次都会取消结果。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-09-03
            • 1970-01-01
            • 2019-04-06
            • 2017-02-09
            • 2012-12-02
            • 2014-11-19
            • 2011-08-26
            相关资源
            最近更新 更多