0

I want to let user capture image from camera and upload to server. So, I need an image file from captured photo.

I searched for many solutions but none of them worked.

I tried following:

        Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
             if (camera_intent.resolveActivity(getActivity().getPackageManager()) != null) {
                                            startActivityForResult(camera_intent, 1);
                }

in onActivityResult:

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent dataIntent) {
        super.onActivityResult(requestCode, resultCode, dataIntent);

        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 1) {
                Bundle dataBundle = dataIntent.getExtras();
                Bitmap imageBitmap = (Bitmap) dataBundle.get("data");
                img_profile.setImageBitmap(imageBitmap); //getting thumbnail and setting to preview image

         try{
                Uri selectedImageUri = dataIntent.getData();

                String[] projection = { MediaStore.Images.Media.DATA };
                Cursor cursor = getActivity().getContentResolver().query(selectedImageUri, projection, null, null,null);

                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();

                String filePath = cursor.getString(column_index);

                profilePicFile = new File(filePath);

                cursor.close();
            }
            catch (Exception e){
                e.printStackTrace();
            }

but my I got nullPinterException saying that Uri is null. I tried almost all solutions on web but didn't work. Can anyone have any solution for this?

Krupal Shah
  • 8,949
  • 11
  • 57
  • 93

1 Answers1

0

I had the same issue. I was able to solve this using FileObserver.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == TAKE_PICTURE_REQUEST && resultCode == RESULT_OK) {
        String thumbnailPath = data.getStringExtra(Intents.EXTRA_THUMBNAIL_FILE_PATH);
        String picturePath = data.getStringExtra(Intents.EXTRA_PICTURE_FILE_PATH);

        processPictureWhenReady(picturePath);
        // TODO: Show the thumbnail to the user while the full picture is being
        // processed.
    }

    super.onActivityResult(requestCode, resultCode, data);
}

private void processPictureWhenReady(final String picturePath) {
    final File pictureFile = new File(picturePath);

    if (pictureFile.exists()) {
        // The picture is ready; process it.
    } else {
        // The file does not exist yet. Before starting the file observer, you
        // can update your UI to let the user know that the application is
        // waiting for the picture (for example, by displaying the thumbnail
        // image and a progress indicator).

        final File parentDirectory = pictureFile.getParentFile();
        FileObserver observer = new FileObserver(parentDirectory.getPath(),
                FileObserver.CLOSE_WRITE | FileObserver.MOVED_TO) {
            // Protect against additional pending events after CLOSE_WRITE
            // or MOVED_TO is handled.
            private boolean isFileWritten;

            @Override
            public void onEvent(int event, String path) {
                if (!isFileWritten) {
                    // For safety, make sure that the file that was created in
                    // the directory is actually the one that we're expecting.
                    File affectedFile = new File(parentDirectory, path);
                    isFileWritten = affectedFile.equals(pictureFile);

                    if (isFileWritten) {
                        stopWatching();

                        // Now that the file is ready, recursively call
                        // processPictureWhenReady again (on the UI thread).
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                processPictureWhenReady(picturePath);
                            }
                        });
                    }
                }
            }
        };
        observer.startWatching();
    }
}

Here is the link that I refer to solve the issue. Its for Google glass but works fine on any Android device.

Varundroid
  • 9,135
  • 14
  • 63
  • 93