Skip to content

Crash when take photo from Camera - Android N #7

@andriistakhov

Description

@andriistakhov

Hi all
It's please add codes below, to fix problem on android N

Also @Tofira thanks for this library.

  1. Add code to AndroidManifest.xml
<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>
  1. Create file provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
            name="external_files"
            path="." />
</paths>
  1. Update file CameraPickerManager.java
protected void sendToExternalApp() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        mProcessingPhotoUri = getImageFileCompat();
        intent.putExtra(MediaStore.EXTRA_OUTPUT, mProcessingPhotoUri);
        activity.startActivityForResult(intent, REQUEST_CODE_SELECT_IMAGE);
    }

    private Uri getImageFileCompat() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            // https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en
            String imageName = activity.getString(R.string.app_name);
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
            String imageFileName = imageName + timeStamp + "_";
            File storageDir = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_DCIM), "Camera");
            try {
                File image = File.createTempFile(
                        imageFileName,  /* prefix */
                        ".jpg",         /* suffix */
                        storageDir      /* directory */
                );
                return FileProvider.getUriForFile(activity,
                        BuildConfig.APPLICATION_ID + ".provider",
                        image);
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;

        } else {
            return getImageFile();
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions