Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

onActivityResult always return null data - Ribbit

When I choose a photo or a video and try to send it, the App crashes. After some investigation I found ouy that the method onActivityResult is always returning null data! I do not know why this is happening. I reviewed the code and it seems correct!

Take a look at the code:

    protected DialogInterface.OnClickListener mDialogListener =
            new DialogInterface.OnClickListener(){
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            switch (i){
                case 0: //Take Picture
                    Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    mMediaUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
                    if(mMediaUri == null){
                        Toast.makeText(MainActivity.this, R.string.error_sdcard, Toast.LENGTH_LONG).show();
                    }
                    else {
                        takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, mMediaUri);
                        startActivityForResult(takePhotoIntent, TAKE_PHOTO_REQUEST);
                    }
                    break;
                case 1: //Take Video
                    Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
                    mMediaUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
                    if(mMediaUri == null){
                        Toast.makeText(MainActivity.this, R.string.error_sdcard, Toast.LENGTH_LONG).show();
                    }
                    else {
                        takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, mMediaUri);
                        takeVideoIntent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 10);
                        takeVideoIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
                        startActivityForResult(takeVideoIntent, TAKE_VIDEO_REQUEST);
                    }
                    break;
                case 2: //Choose Picture
                    Intent choosePictureIntent = new Intent(Intent.ACTION_GET_CONTENT);
                    choosePictureIntent.setType("image/*");
                    startActivityForResult(choosePictureIntent, CHOOSE_PHOTO_REQUEST);
                    break;
                case 3: //Choose Video
                    Intent chooseVideoIntent = new Intent(Intent.ACTION_GET_CONTENT);
                    chooseVideoIntent.setType("video/*");
                    Toast.makeText(MainActivity.this, R.string.warning_video_size, Toast.LENGTH_LONG).show();
                    startActivityForResult(chooseVideoIntent, CHOOSE_VIDEO_REQUEST);
                    break;
            }
        }

After that in the onActivityResult:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode == RESULT_OK){
            if(requestCode == TAKE_PHOTO_REQUEST || requestCode == TAKE_VIDEO_REQUEST) {
                // Take file from the Gallery
                if(data == null){
                    Toast.makeText(this, getText(R.string.general_error), Toast.LENGTH_LONG).show();
                }
                else {
                    mMediaUri = data.getData();
                }
                if(requestCode == TAKE_VIDEO_REQUEST){
                    // Make sure video is less than 10Mb
                    int fileSize = 0;
                    InputStream inputStream = null;
                    try {
                        inputStream = getContentResolver().openInputStream(mMediaUri);
                        fileSize = inputStream.available();
                    }
                    catch (FileNotFoundException e){
                        Toast.makeText(this, getText(R.string.file_error), Toast.LENGTH_LONG).show();
                        return;
                    }
                    catch (IOException e){
                        Toast.makeText(this, getText(R.string.file_error), Toast.LENGTH_LONG).show();
                        return;
                    }
                    finally {
                        try {
                            inputStream.close();
                        } catch (IOException e) {
                            // Leave blank
                        }
                    }
                    if( fileSize >= FILE_SIZE_LIMIT){
                        Toast.makeText(this, getText(R.string.file_size), Toast.LENGTH_LONG).show();
                        return;
                    }
                }
            }
            else {
                // Add file to the Gallery
                Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                mediaScanIntent.setData(mMediaUri);
                sendBroadcast(mediaScanIntent);
            }

            Intent recipientsIntent = new Intent(this, RecipientsActivity.class);
            recipientsIntent.setData(mMediaUri);

            String fileType;
            if(requestCode == TAKE_PHOTO_REQUEST || requestCode == CHOOSE_PHOTO_REQUEST){
                fileType = ParseConstants.TYPE_IMAGE;
            }
            else {
                fileType = ParseConstants.TYPE_VIDEO;
            }
            recipientsIntent.putExtra(ParseConstants.KEY_FILE_TYPE, fileType);
            startActivity(recipientsIntent);

        }
        else if (resultCode != RESULT_CANCELED){
            Toast.makeText(this, R.string.general_error, Toast.LENGTH_LONG).show();
        }
    }

Could you guys help me? Ben!?!? Heeelp!

Thanks!

4 Answers

Hey Sebastian! One thing I noticed is that you're using TAKE_PHOTO_REQUEST and TAKE_VIDEO_REQUEST instead of CHOOSE_PHOTO_REQUEST AND CHOOSE_VIDEO_REQUEST in the 5th and 13th line of your onActivityResult method. See if that fixes the issue for you.

Harry James
Harry James
14,780 Points

Have you not done it the wrong way round?

For me, my

 if(requestCode == TAKE_PHOTO_REQUEST || requestCode == TAKE_VIDEO_REQUEST) {

is actually

 if(requestCode == PICK_PHOTO_REQUEST || requestCode == PICK_VIDEO_REQUEST) {

(You have written TAKE instead of PICK).

Yes, I notice that. I changed the name on purpose but I follow the same name in the rest of the code. ;)

Any other suggestions? The real thing is the data is always empty! :S

I am using a Moto X with Kitkat 4.4.4. The strange thing is that in the emulator it works fine (Kitkat 4.4.2), maybe is something related with the Andrid version?

Yeah, I noticed that you named your variables differently. However, in the lines I pointed out, it looks like you are using the wrong variables. In my answer, I used your naming convention to avoid any confusion. Try to see if that helps solve the problem.

You were right Kristen Law!!! Thank you very much!