Android 7.0 open camera (rights and content provider)

Most of this article comes from the second line of code, only part of the permission problems are changed and explained
The main problems are Android 6.0 permission and 7.0 URL.

  1. You need to set permissions first
    <! -- camera -- >
    <uses-permission android:name="android.permission.CAMERA" />
    <! -- get sd card write permission for file upload and download -- >
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    This is the permission to use camera and modify memory

    2. Then the uri of the file
    Android 7.0 requires that the uri of the file must not be exposed. Otherwise, an error fileuriexposexception error will be reported. The uri must be encapsulated by fileProvider and then transferred to the outside. The configuration is as follows
    ① Set in < manifest >

<provider          android:name=""         android:authorities="com.example.mbmob.fileprovider"
 <!--HereauthoritiesYou can set any value, but different applications must be different, or you will not be able to install because of duplicate databases -->
<!--Must be false ,It means not to be shared. This is fileprovide requirement-->
<meta-data            android:name=""
android:resource="@xml/file_paths" />
<!--meta appoint uri And refers to a@xml/file_paths resources-->
② Next, right-click res, new -- directory, create xml directory, and generate file_paths, copy the contents as follows
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="">
    <external-path  name ="my_images"  path=""/>
    <! -- any name and empty paths indicate that the whole sd card can be shared (red error may be reported, regardless) -- >

3 relevant codes for taking photos

First of all, check permissions, camera permissions and access to memory card permissions

if((ContextCompat.checkSelfPermission(getActivity().getBaseContex(),Manifest.permission.CAMERA)!=PackageManager.PERMISSION_GRANTED)||ContextCompat.checkSelfPermission(getActivity().getBaseContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) !=PackageManager.PERMISSION_GRANTED)) {
                        //If there is no authorization, request authorization
       ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1311);
       //In the String array is the permission applied for, the first is camera, the second is memory modification, and the last parameter is the return value of the authorization application, which I set1311
                    } else {
   public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {  
           case 1311:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED&& grantResults[1] == PackageManager.PERMISSION_GRANTED) {
            } else {
                Toast.makeText(getContext(),"No permission to open camera",Toast.LENGTH_SHORT).show();
          public void zhaoxiang() {
    Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);    headImage = new File(Environment.getExternalStorageDirectory() + "/mbmob/", year + "-" + month + "-" + day + i.getName() + "head.jpg");

    //Create a file, the first exponential path, and the second is the image name. The path is a file named mbmob in sd card. The headImage is an empty file I declared in advance
    //If you don't add / mbob / this sentence, you will get a place in the mobile phone where the application cache is specially placed. That way is to get this location
    try {
        if (headImage.exists()) {
        //If the original file is deleted
    } catch (IOException e) {
    if (Build.VERSION.SDK_INT >= 24) {
        //Judge Android version higher than 7.0
        imageUri = FileProvider.getUriForFile(getActivity(), "com.example.mbmob.fileprovider", headImage);
        // The first is content, the second is the author set in the provider, and the third is the File object
    } else {
        //Less than 7.0
        imageUri = Uri.fromFile(headImage);
        //Get picture path

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
    //Specify the output address of the picture
    startActivityForResult(intent, TAKE_PHOTO);

public void onActivityResult(int requestCode, int resultCode, Intent data) {

    switch (requestCode) {
        case TAKE_PHOTO:
            //When the camera is finished, it will return. Call this
            if (resultCode == RESULT_OK) {

               //At this time, the obtained headimage is saved. How to use your own definition here




Tags: Android FileProvider xml encoding

Posted on Sun, 31 May 2020 06:47:55 -0700 by newbie_07