-3

My app crashed when open camera in Android 7.0

        File captureFilePath = new File(cachePath, CommonUtil.getDateTimeForFileName(System.currentTimeMillis())+".jpg");
        Uri uri = Uri.fromFile(captureFilePath);
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);
        startActivityForResult(intent, REQUEST_CAPTURE_IMAGE);
        CAPTURE_IMAGE_PATH = captureFilePath.getAbsolutePath();
halfer
  • 19,824
  • 17
  • 99
  • 186
임정택
  • 3
  • 1

1 Answers1

0

If your targetSdkVersion >= 24, then we have to use FileProvider class to give access to the particular file or folder to make them accessible for other apps.

1) First Add a FileProvider tag in AndroidManifest.xml under tag as below:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    <application
        ...
        <provider
            android:name=".GenericFileProvider"
            android:authorities="${applicationId}.my.package.name.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>
    </application>
</manifest>

2) Then create a provider_paths.xml file in res/xml folder. Folder may be needed to created if it doesn't exist.

<paths>
    <external-path name="external_files" path="."/>
</paths>

3) Now edit your activity class file as below:

Uri uri = Uri.fromFile(captureFilePath);

to

Uri uri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID, captureFilePath);
Arbaz Alam
  • 1,172
  • 1
  • 14
  • 24
  • just add... My app crashed before it runs. – 임정택 Feb 27 '18 at 00:49
  • @임정택 Are you able resolve this or not? as you marked this question as correct I assume that you are able to resolve your issue. Please let me know if you face any issue. – Arbaz Alam Feb 27 '18 at 05:47