2013-07-24 31 views
8
@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; 
} 

我检查值文件输出乌里,两者都是细并且将所捕获的图像实际上在那个位置存在。onActivityResult返回null数据的图像捕获

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

编辑:

我查了一下这个问题:

onActivityResult returns with data = null

它说:

当你通过传递EXTRAOUTPUT用相机意图 数据参数保存图像onActivityResult里面总是返回null。所以, 而不是使用数据检索图像,使用文件路径 检索位图。

也许该解决方案将为我工作。但是,对于相同的场景,上面的代码是一个工作代码。

回答

18

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

i.putExtra(MediaStore.EXTRA_OUTPUT, output); 

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

+0

是的,刚刚看到帖子。但相同的代码在过去6个月工作,为什么突然现在它的行为是这样的:( –

+1

不幸的是,我不是这个主题的专家把我猜测它的硬件设备相关的特定功能。在三星设备上有不同的行为:http://stackoverflow.com/q/8248327/1965084 – alex

0

只需将此代码放入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变量和URI的图片路径selectedImage变量。

1

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

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(); 
       } 
      } 
1

尝试下面的代码

 { 
      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()); 
0

如果你的活动已在您的清单launchmode作为singleInstance,那么你将面临这个问题。尝试改变它。因为它每次取消结果。

相关问题