Android calls the system camera to take photos

Refrence:

  1. Android 11 Camera cropping adaptation
  2. Android calls the camera to take photos
  3. Android FileProvider parsing and stomping guide
  4. Android call camera in Java. Lang. IllegalArgumentException: Failed to find configured root that contains

Android call system camera photo adaptation mainly experienced 6.0, 7.0, 10 and 11 of these major versions:

  1. 6.0 deals primarily with runtime permissions
  2. 7.0 FileProvider sharing
  3. 10 and 11 are partitioned storage

Basic steps

Androidmanifest.xml configure the provider

    <application
        .
        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.fileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>
    </application>
Copy the code

Among them:

  1. Android :authorities [appId].fileProvider
  2. Android: Exported can only be false
  3. Android: grantUriPermissions = “true” said authorization Uri permissions, and must to true
  4. Set the specified file directory in meta-data to reference provider_Paths file

Add file_paths.xml

The label code The path
<cache-path> context.getCacheDir() / data/data / < package name > / cache
<files-path> context.getFilesDir() / data/data / < package name > / cache
<external-path> Environment.getExternalStorageDirectory() /storage/emulate/0
<external-cache-path> context.getExternalCacheDir() / storage/emulate / 0 / Android/data / < package name > / cache
<external-files-path> context.getExternalFilesDir() / storage/emulate / 0 / Android/data / < package name > / files
<external-media-path> context.getExternalMediaDirs() / storage/emulate / 0 / Android/data / < package name >

External-path and external-files-path are commonly used. Name and path are prepared based on your requirements

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path
        name="externalPath"
        path=". / 000" />
</paths>
Copy the code

This example indicates that the file/folder specified in the external-path path has permissions to be accessed. This means that /storage/emulate/0/000 has permissions to be accessed.

Call system camera with intent

A simple example:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File outFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + 000 "/ /" + System.currentTimeMillis() + ".jpg");
Uri uri = FileProvider.getUriForFile(this, getPackageName(getApplicationContext()) + ".fileProvider", outFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivity(intent);
Copy the code

TODO: Calls the system camera to take pictures and crop