Skip to content Skip to sidebar Skip to footer

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

Solution 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:

protectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == REQUEST_CODE) {
        if (resultCode == Activity.RESULT_OK) {
            String[] fileColumn = { MediaStore.Images.Media.DATA };
            Cursorcursor= getContentResolver().query(imageUri,
                fileColumn, null, null, null);
            StringcontentPath=null;
            if (cursor.moveToFirst()) {
                contentPath = cursor.getString(cursor
                    .getColumnIndex(fileColumn[0]));

                Bitmapbmp= BitmapFactory.decodeFile(contentPath);
                ImageViewimg= (ImageView) findViewById(R.id.imageView1);
                img.setImageBitmap(bmp);


            } elseif (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

Solution 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..

Post a Comment for "Onactivityresult Returns With Data = Null"