onActivityResult returns with data = null

匿名 (未验证) 提交于 2019-12-03 02:52:02

问题:

Ok so this here is the intent I am sending

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name startActivityForResult(intent, REQUEST_CODE); 

And then in the onActivityResult I am doing this:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {         Log.i("Intent name:",data.toString());         if (requestCode == REQUEST_CODE){             if (resultCode == Activity.RESULT_OK){                 Toast.makeText(this, "Image saved to \n" + fileUri.toString() , Toast.LENGTH_LONG).show();                 Toast.makeText(this, "Result Code: " + resultCode , Toast.LENGTH_LONG).show();                 //Bitmap mBitMap = BitmapFactory.decodeFile(data.getData().toString());                 //imageView.setImageBitmap(mBitMap);             }             else if (resultCode == RESULT_CANCELED){                 Toast.makeText(this, "Capture Cancelled", Toast.LENGTH_LONG).show();             }             else {                 Toast.makeText(this, "Capture failed", Toast.LENGTH_LONG).show();             }          }          super.onActivityResult(requestCode, resultCode, data);     }   

The LogCat is showing a NullPointerException at the line that says Image Saved....
And also this:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null}

This happens whether i try to use the data object or the fileUri field of my class.
Why is data being returned null?
Why is it that even though I am using a field of the class i still get the same error?

回答1:

Whenever you save an image by passing EXTRAOUTPUT with camera intent ie

intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 

in a file, the data parameter inside the onActivityResult always return null. So, instead of using data to retrieve the image , use the filepath to retrieve the Bitmap.

So onActivityResult would be something like this:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {      if (requestCode == REQUEST_CODE) {         if (resultCode == Activity.RESULT_OK) {             String[] fileColumn = { MediaStore.Images.Media.DATA };             Cursor cursor = getContentResolver().query(imageUri,                 fileColumn, null, null, null);             String contentPath = null;             if (cursor.moveToFirst()) {                 contentPath = cursor.getString(cursor                     .getColumnIndex(fileColumn[0]));                  Bitmap bmp = BitmapFactory.decodeFile(contentPath);                 ImageView img = (ImageView) findViewById(R.id.imageView1);                 img.setImageBitmap(bmp);               } else if (resultCode == RESULT_CANCELED) {                 Toast.makeText(this, "Capture Cancelled", Toast.LENGTH_LONG)                 .show();             } else {                 Toast.makeText(this, "Capture failed", Toast.LENGTH_LONG)                 .show();             }          }          super.onActivityResult(requestCode, resultCode, data);     } 

Make sure that you have taken imageUri or fileUri as a global variable so that you can access it inside onActivityResult as well. Best of luck



回答2:

The correct/preferred way to handle data in these cases would be as:

In called Activity set data to the Intent , then setResult code as RESULT_OK and then finish that activity.

In this recieving activity , check the result code.. and retrieve data from Intent variable as :intent.getExtra("... "); //The variables which you have set in the child activity that has been closed now..



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!