Realization method of taking pictures and reading photo albums adapted to AndroidQ

  • 2021-12-04 19:41:00
  • OfStack

It has been a long time since Google released Android Q, and Huawei's application market has required to adapt to Android Q, so we also need to adapt to Android Q.

Let's talk about the new features we use in this section first

Android Q file storage mechanism has been modified to sandbox mode, similar to iOS Applications can only access files under their own sandbox and public media files

If you want to know more about the new special effects of Android Q, you can go to the official document
In this place, we record the function of Android Q version to take photos and save them to the photo album.

Authority issue

Android Q no longer needs to apply for file read and write permissions, and can read and write its own sandbox files and public media files by default. Because photo albums are public media files, Android Q can no longer dynamically apply for permission. However, the following versions of Android and Q must apply for permission to access them.

Photographing

Pictures are saved normally

Photography: Before Android Q, save the picture to the specified folder, and notify the photo album to refresh after taking pictures

Photo code


//  Photo storage path 
    File fileDir = new File(Environment.getExternalStorageDirectory(), "Pictures");
    if (!fileDir.exists()) {
      fileDir.mkdir();
    }

 // Picture name 
    String fileName = "/IMG_" + System.currentTimeMillis() + ".jpg";
    // Jump to camera 
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    Uri uri;
    // Adaptation Android N Later versions 
    if (Build.VERSION.SDK_INT >= 24) {
      uri = FileProvider.getUriForFile(this, ImagePickerProvider.getFileProviderName(this), new File(mFilePath));
    } else {
    // Adaptation Android N Before version 
      uri = Uri.fromFile(new File(mFilePath));
    }
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
    // Jump, need to be in onActivityResult Process 
    startActivityForResult(intent, 1111);

Refresh album code


sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + mFilePath)));

The above is the general photo code before Android Q. If necessary, you can copy it directly

Save the picture in the sandbox

The operation files in the sandbox no longer need to apply for permission The new folder in the sandbox can only be created in the subfolder specified by the system

Android Q version is used to store pictures in sandbox files, and the gallery cannot be refreshed and displayed


//  Get the picture sandbox folder 
    File PICTURES = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    // Picture name 
    mFileName = "IMG_" + System.currentTimeMillis() + ".jpg";
    // Picture path 
    mFilePath = PICTURES.getAbsolutePath()+"/"+mFileName;
    // Jump to camera 
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    Uri uri;
    if (Build.VERSION.SDK_INT >= 24) {
      uri = FileProvider.getUriForFile(this, ImagePickerProvider.getFileProviderName(this), new File(mFilePath));
    } else {
      uri = Uri.fromFile(new File(mFilePath));
    }
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
    startActivityForResult(intent, 1111);

This does not need to call the code to refresh the photo album library, because it cannot be displayed if it is refreshed

Careful comparison 1 shows that there is basically no difference between the two, only the file storage path is different, because the storage has been changed in the new version of Android Q.

Save pictures to a public folder


  //-------------------------
    // Photo storage path 
    // For adaptation Android Q Below version 
    File fileDir = new File(Environment.getExternalStorageDirectory(), "Pictures");
    if (!fileDir.exists()) {
      fileDir.mkdir();
    }

    String fileName = "/IMG_" + System.currentTimeMillis() + ".jpg";
    //--------------------------
    //--------------------------
    // Setting parameters 
    Uri uri = null;
//  Set the save parameters to ContentValues Medium 
    ContentValues contentValues = new ContentValues();
    // Set the file name 
    contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, fileName);
    // Compatible Android Q And the following versions 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
      //android Q Is no longer used in DATA Field, instead of using the RELATIVE_PATH Substitute 
      //RELATIVE_PATH Is a relative path, not an absolute path 
      //DCIM It is a system folder. You can view the system folder in the file manager that comes with the system. You can't write a name that doesn't exist 
      contentValues.put(MediaStore.Images.Media.RELATIVE_PATH, "DCIM/Pictures");
    } else {
    //Android Q The following versions 
      contentValues.put(MediaStore.Images.Media.DATA, mFilePath);
    }
    // Set the file type 
    contentValues.put(MediaStore.Images.Media.MIME_TYPE, "image/JPEG");
    // Execute insert Action to add files to the system folder 
    //EXTERNAL_CONTENT_URI Represents external memory, and the value is unchanged 
    uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
//     If generated uri The file was added successfully 
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
    startActivityForResult(intent, 1111);

Using this code does not need to refresh the photo album library, it will refresh automatically.
The operation of saving to public files is relatively troublesome, but it is also good.


Related articles: