0

I am trying to integrate taking a picture in Android. While running the application I am getting and error "Unfortunately, Camera has stopped", but my application didn't crash. I found this issue in Android "KITKAT". Here is the sample code I am using to take picture,

Using this function I am taking a picture,

private void take_picture_intent() {
        Intent takePictureIntent = (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT)
                ? new Intent(MediaStore.ACTION_IMAGE_CAPTURE_SECURE)
                : new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(this.getActivity().getPackageManager()) != null) {
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (photoFile != null) {
                photoURI = FileProvider.getUriForFile(this.getActivity(), "com.android.myapplication.fileprovider", photoFile);
                Log.e(TAG, "photoFile: "+photoFile+" ,URI : "+photoURI.getPath());
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                getActivity().startActivityForResult(takePictureIntent, SelectMedia.IMAGE_CAPTURE_AT_THE_DOOR);
            }
        }
    }

This function is used to generate Image path,

private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        if (!storageDir.exists())
            storageDir.mkdirs();
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );
        // Save a file: path for use with ACTION_VIEW intents
        mCurrentPhotoPath = image.getAbsolutePath();
        return image;
    }

Here is the Log,

10-11 12:35:12.691 18815-18815/? E/AndroidRuntime: FATAL EXCEPTION: main
 Process: com.sec.android.app.camera, PID: 18815
 java.lang.SecurityException: Permission Denial: opening provider android.support.v4.content.FileProvider from 
 ProcessRecord{42d78c60 18815:com.sec.android.app.camera/u0a84} (pid=18815, uid=10084) that is not exported from uid 10786
 at android.os.Parcel.readException(Parcel.java:1472)
 at android.os.Parcel.readException(Parcel.java:1426)
 at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:3201)
 at android.app.ActivityThread.acquireProvider(ActivityThread.java:4824)
 at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2532)
 at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1425)
 at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:906)
 at android.content.ContentResolver.openOutputStream(ContentResolver.java:669)
 at android.content.ContentResolver.openOutputStream(ContentResolver.java:645)
 at com.sec.android.app.camera.Camera$LastContentUriCallback.onCompleted(Camera.java:21369)
 at com.sec.android.app.camera.Camera.onLaunchGalleryForImage(Camera.java:12409)
 at com.sec.android.app.camera.Camera.onImageStoringCompleted(Camera.java:11140)
 at com.sec.android.app.camera.CommonEngine.imageStoringCompleted(CommonEngine.java:6954)
 at com.sec.android.app.camera.CeStateInitialized.handleMessage(CeStateInitialized.java:47)
 at com.sec.android.app.camera.CommonEngine$StateMessageHandler.handleMessage(CommonEngine.java:957)
 at android.os.Handler.dispatchMessage(Handler.java:102)
 at android.os.Looper.loop(Looper.java:136)
 at android.app.ActivityThread.main(ActivityThread.java:5590)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:515)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
 at dalvik.system.NativeStart.main(Native Method)
10-11 12:35:13.021 600-19132/? E/android.os.Debug: !@Dumpstate > sdumpstate -k -t -z -d -m 18815 -o /data/log/dumpstate_app_error
Haem
  • 929
  • 6
  • 15
  • 31
SHIDHIN TS
  • 1,557
  • 3
  • 26
  • 58

2 Answers2

7

I found this issue is due to conversion of URI. I have resolved this issue by replacing

FileProvider.getUriForFile(getApplicationContext(), "com.android.myapplication.fileprovider", photoFile);

By Adding a condition,

if ((Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT))
     photoURI = FileProvider.getUriForFile(getApplicationContext(), "com.android.myapplication.fileprovider", photoFile);
else
     photoURI = Uri.fromFile(photoFile);

on take_picture_intent() function.

Also In my Manifest I am have removed permission,

<uses-permission android:name="android.permission.CAMERA" />
SHIDHIN TS
  • 1,557
  • 3
  • 26
  • 58
  • Damn, I've spent hours working on this! Thanks, you saved me! The code was working on my other phones, but not Nexus 5 running 4.4.4. Google should REALLY put a note on their tutorial page for this. – tingyik90 Jan 31 '18 at 15:02
0

Add the permission for pre-lollipop versions of specific package name, after that you can able to access it.

getApplicationContext().grantUriPermission(getCallingPackage(), 
      contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
Sharath kumar
  • 4,064
  • 1
  • 14
  • 20
  • Not working, I have added the line after, photoURI = FileProvider.getUriForFile(this.getActivity(), "com.android.myapplication.fileprovider", photoFile); – SHIDHIN TS Oct 11 '17 at 08:19
  • Try to add at the beginning of the method since it is a permission.This should be added before "photoURI = FileProvider.getUriForFile(this.getActivity(), "com.android.myapplication.fileprovider", photoFile)" – Sharath kumar Oct 11 '17 at 08:20