0

There are some questions in Stackoverflow about this subject, however, none of them solved actually my problem.

I have a helper class where I activate the camera to take a picture. this is the code to call the activity:

        private void dispatchTakePictureIntent() {
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (takePictureIntent.resolveActivity(activity.getPackageManager()) != null) {
                Image img = new Image();
                // crea el archivo en donde se almacenará la foto
                File photoFile = null;
                try {
                    photoFile = img.createImageFile(activity.getApplicationContext());
                } catch (IOException e) {
                    Log.e("Photo", e.getMessage());
                }

                if (photoFile != null) {
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                            Uri.fromFile(photoFile));
                    activity.startActivityForResult(takePictureIntent, ((FormActivity) activity).REQUEST_TAKE_PHOTO);
                }
            }

When I take the picture and press "OK" option, I saw that the image is correctly stored in the path (I find it when browsing with the phone file manager). When pressing "OK", the onActivityResult of the activity is actually called, however with null Intent, so I don't know how to get the file name so that I can resize it and then store it in a database.

How can i do it? Thanks

EDIT:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
        try {
            Bundle extras = data.getExtras();
            Uri file = (Uri) extras.get(MediaStore.EXTRA_OUTPUT);
            Image img = new Image(file.getPath());
            img.resizeImage(200, 200);
            //Bitmap imageBitmap = (Bitmap) extras.get("data");
            //Image img = new Image();
            //mImageView.setImageBitmap(imageBitmap);
        }
        catch (Exception e)
        {
            Log.e("TDC@", e.getMessage());
        }
    }
    else
        super.onActivityResult(requestCode, resultCode, data);
}
jstuardo
  • 3,901
  • 14
  • 61
  • 136
  • link your code for onActivityResult pls – Lucas Crawford Oct 21 '15 at 21:35
  • it is not relevant since "data" parameter on calling is null, however, I have modified the question. – jstuardo Oct 21 '15 at 21:45
  • Question was not answered in the link provided by @CommonsWare. It is a fact that I know the path for the image, but this is known by the helper class, and not by the activity, and I don't want to use some sort of static variable because this would result in a poorly structured code. – jstuardo Oct 21 '15 at 21:49
  • Call `startActivityForResult()` in the `Activity`. Hold onto the path in the `Activity` (and the saved instance state `Bundle`, in case of a configuration change), and use that path in `onActivityResult()`. Of course, you are welcome to try to convince all the world's camera app developers to change their apps to accommodate your wishes, then use a flux capacitor to go back in time and change all existing devices' pre-installed apps. You will find it more practical to hold onto the path. – CommonsWare Oct 21 '15 at 21:54
  • Actually his link does help you... http://developer.android.com/intl/zh-tw/reference/android/provider/MediaStore.html#ACTION_IMAGE_CAPTURE read that. No intent is returned because the bitmap lives in your MediaStore.EXTRA_OUTPUT location. – Lucas Crawford Oct 21 '15 at 21:57
  • I did not understand you... I have done what you explained however I am not sure what you mean with "Hold onto the path in the Activity" – jstuardo Oct 21 '15 at 21:58
  • Make `photoFile` be a field in your activity, instead of a local variable in your `dispatchTakePictureIntent()` method. – CommonsWare Oct 21 '15 at 22:00
  • @Lucas Crawford, I know where the file is located, but I don't know the file name – jstuardo Oct 21 '15 at 22:00
  • I think if you do as CommonsWare suggests, you will find your problem is solved – Lucas Crawford Oct 21 '15 at 22:09
  • @CommonsWare yes.. I have finally done that. Thanks. – jstuardo Oct 22 '15 at 11:36

0 Answers0