Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onActivityResult returns with data = null

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?

like image 705
prometheuspk Avatar asked Feb 10 '12 17:02

prometheuspk


1 Answers

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

like image 177
Raman Ghai Avatar answered Oct 30 '22 02:10

Raman Ghai