OnActivityResult returns null data for image capture

@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; } 

I checked the values ​​for the Uri file and output , both of them are beautiful, and the captured image really exists in this place .

But the data returned in onActivityResult is always null even after image capture.

EDIT:

I checked this question:

onActivityResult returns with data = null

which reads:

Whenever you save an image by passing EXTRAOUTPUT with the intent of the camera, the data parameter inside onActivityResult always returns null. So, instead of using data to extract the image, use the file path to get the bitmap.

and maybe this solution will work for me. But the above code has been working code so far in the same scenario.

+8
android onactivityresult
source share
5 answers

According to this post, the data is null when you pre-insert uri. This means that you have already defined your output uri here:

  i.putExtra(MediaStore.EXTRA_OUTPUT, output); 

So, when you get Activity.RESULT_OK; just upload the resulting photo to its famous URL.

+18
source share

Try this code, it works for me.

 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
source share

Try the following code

  { 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()); 
+1
source share

Just put this code in your onActivityResult. The same problem that I encountered on some devices, and this solved my problem. Hope this also helps you.

 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; } 

You will get the image path in the picturePath variable and Uri in the selectedImage variable.

0
source share

If your activity has a run mode as singleInstance in your manifest, you will run into this problem. Try changing it. Because it cancels the result every time.

0
source share

All Articles