0

I am trying to create and Android application that takes a photo or uploads a photo to the server. My main objective is to get the URI of the photo which is returned in the intent. I have followed the steps from [1]. The problem is that on a phone with Lollipop, version 5.1.1 it works fine and the intent returns the URI of the photo, but on a phone which has Jelly Bean, version 4.2.1, the URI returned in the intent is null.

Here is my code

Create View and set listeners to buttons

static final int REQUEST_TAKE_PHOTO = 1;
static final int REQUEST_GET_PHOTO = 2;   

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_home_page, container, false);

    camera = (FloatingActionButton) view.findViewById(R.id.take_photo);
    upload = (FloatingActionButton) view.findViewById(R.id.upload_photo);
    photo = (ImageView) view.findViewById(R.id.photo);

    camera.setOnClickListener(takePhoto);
    upload.setOnClickListener(uploadPhoto);

    return view;
}

Take Picture Action

   View.OnClickListener takePhoto = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
            }
        }
    };

Upload from gallery listener

View.OnClickListener uploadPhoto = new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
        if (photoPickerIntent.resolveActivity(getActivity().getPackageManager()) != null) {
            photoPickerIntent.setType("image/*");
            startActivityForResult(photoPickerIntent, REQUEST_GET_PHOTO);
        }
    }
};

Handle Intent Result

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == Activity.RESULT_OK && (requestCode == REQUEST_TAKE_PHOTO || requestCode == REQUEST_GET_PHOTO)) {

        Uri imageUri = data.getData();

        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getApplicationContext().getContentResolver(), imageUri);
            String filePath = getPath(imageUri);
            bitmap = BitmapRotator.rotateBitmap(bitmap, filePath);

            photo.setImageBitmap(bitmap);
            UploadPhotoController.uploadPhoto(filePath);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (PhotoNotFoundException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }
}

data.getData() is null thus I can't get the uri, or the file path of the image.

Thank you.

[1] http://developer.android.com/training/camera/photobasics.html

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
Ancuta
  • 183
  • 2
  • 6
  • 15
  • Could you add the whole function instead of this part only? – rafaelc Dec 13 '15 at 20:27
  • I have added the hole function – Ancuta Dec 13 '15 at 20:59
  • This is likely a bad implementation of the Camera intent on your JB device. Try to install [Open Camera](https://play.google.com/store/apps/details?id=net.sourceforge.opencamera&hl=en) on this device, and check if it behaves different. – Alex Cohn Dec 13 '15 at 22:31
  • 1
    Also https://stackoverflow.com/questions/34047500/android-jelly-bean-camera-return-null-uri/34048243. In short, there is not supposed to be a returned `Uri`. Since you are not providing `EXTRA_OUTPUT`, your only result from `ACTION_IMAGE_CAPTURE` is `data.getExtra("data")`, which will be a thumbnail image. – CommonsWare Dec 13 '15 at 22:55
  • Thank you very much for your help. I have solved my issue – Ancuta Dec 14 '15 at 11:24
  • Actually in Android N you cant use URI,dont work – David Hackro May 08 '17 at 12:22

0 Answers0