1

I have already read this post: Get uri from camera intent in android but it didn't help me.

To handle the camera request I've used this code:

    public void startCamera() {
    if (PermissionUtils.requestPermission(
            this,
            CAMERA_PERMISSIONS_REQUEST,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA)) {
        //Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        Uri photoUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", getCameraFile());
        intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        startActivityForResult(intent, CAMERA_IMAGE_REQUEST);
    }
}


 public File getCameraFile() {
    File dir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    return new File(dir, FILE_NAME);
}


    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == CAMERA_IMAGE_REQUEST && resultCode == RESULT_OK) {
        Uri photoUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", getCameraFile());
        uploadImage(photoUri);
    }
}

When I call the function "startCamera" the app crashes giving me error at the line:

Uri photoUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", getCameraFile());"

Any help? Thank you!

Jr Antonio
  • 193
  • 1
  • 4
  • 20
  • Possible duplicate of [Get uri from camera intent in android](https://stackoverflow.com/questions/21388586/get-uri-from-camera-intent-in-android) – M.A.K. Ripon Oct 07 '18 at 09:14
  • 1
    @M.A.K.Ripon ..I have already said that that post didn't answer my question, I tried to apply the solutions under that post but those doesn't work. – Jr Antonio Oct 07 '18 at 09:17
  • Try this one https://stackoverflow.com/questions/47448297/how-to-get-uri-of-captured-photo – M.A.K. Ripon Oct 07 '18 at 09:19
  • Helpful but I can't fix my problem... all these tutorials are made for older versions of android... – Jr Antonio Oct 07 '18 at 09:43
  • Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/q/23353173/115145. My guess is that your `FileProvider` XML metadata is misconfigured. – CommonsWare Oct 07 '18 at 11:17
  • as courtesy to the community, please supply the 'FIXED' part as an [**Answer**](https://stackoverflow.com/questions/52687052/how-to-get-the-uri-from-a-camera-intent#answers-header) and later accept your own answer. – Alex Cohn Oct 08 '18 at 06:14

1 Answers1

0

FIXED by adding to the "AndroidManifest" this:

        <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths" />
    </provider>
Jr Antonio
  • 193
  • 1
  • 4
  • 20