1

I've never tried to take pictures in one of my projects before, so I apologize for possibly asking a stupid question. Basically, I'm using the following code to try and take a picture and then save it in a temporary file. I wasn't getting any errors, but nothing is happening. I've found that the IOException in the last part of the code is being thrown. if you know why that might be, I would really appreciate your help. Thank you! Permissions used:

<uses-feature android:name="android.hardware.camera"
                              android:required="true" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Code:

 import android.content.Intent;
    import android.net.Uri;
    import android.os.Environment;
    import android.provider.MediaStore;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;

    import java.io.File;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Date;

    public class MainActivity extends AppCompatActivity {
        static final int REQUEST_IMAGE_CAPTURE = 1;
        private static final String TAG = "MainActivity";
        String mCurrentPhotoPath;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button takePicButton = (Button)findViewById(R.id.takePicButton);
            takePicButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dispatchTakePictureIntent();
                }
            });
        }

        private File createImageFile() throws IOException {
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

            String imageFileName = "JPEG_" + timeStamp + "_";
            File storageDir = Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES);
            File image = File.createTempFile(
                    imageFileName,  /* prefix */
                    ".jpg",         /* suffix */
                    storageDir      /* directory */
            );

            mCurrentPhotoPath = "file:" + image.getAbsolutePath();
            return image;
        }
        static final int REQUEST_TAKE_PHOTO = 1;

        private void dispatchTakePictureIntent() {
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                File photoFile = null;
                try {
                    photoFile = createImageFile();
                } catch (IOException ex) {
                    Log.i(TAG, "Searchable, Catch error ocurred");

                }
                if (photoFile != null) {
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                            Uri.fromFile(photoFile));
                    startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
                }
            }
        }
    }
Jake
  • 37
  • 6
  • post exception message... – MilanNz Apr 16 '16 at 23:00
  • Do not catch an exception without logging the exception itself. Replace `Log.i(TAG, "Searchable, Catch error ocurred")` with `Log.e(TAG, "Searchable, Catch error ocurred", ex)`. This way, you will get the Java stack trace of the exception and can learn what is wrong. – CommonsWare Apr 16 '16 at 23:29
  • @MilanNz error is: E/MainActivity: java.io.IOException: open failed: EACCES (Permission denied) – Jake Apr 17 '16 at 01:38
  • @CommonsWare error is: E/MainActivity: java.io.IOException: open failed: EACCES (Permission denied) – Jake Apr 17 '16 at 01:38

1 Answers1

1

Ok, you are using Android 6.0 or higher as target. From Android 6.0 we have run time permissions which mean that it is not enough to just add storage permission in AndroidManifest.xml.

For more information see link below: http://developer.android.com/training/permissions/requesting.html

It is easy to implement and you have you need in link above.

BR Milan

MilanNz
  • 1,282
  • 3
  • 12
  • 27