SoFunction
Updated on 2025-04-10

Implementation method for taking photos and reading albums with AndroidQ

Google has been releasing Android Q version for a long time. Huawei’s application market has already required that it be adapted to Android Q version, so we also need to adapt to Android Q.

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

  • Android Q file storage mechanism has been modified into sandbox mode, similar to iOS
  • The application can only access files and public media files under its own sandbox

If you want to know more about the new Android Q version of the special effects, you can goOfficial Documentation
We will record the function of taking photos and saving them to the album in this place.

Permissions issues

Android Q no longer requires application for file read and write permissions, and can read and write its own sandbox files and public media files by default. Because the album belongs to public media files, Android Q can no longer apply for permission dynamically. However, Android Q must apply for permission to access it.

Photograph

Pictures are saved normally

Take photos: used in the previous version of Android Q. Save the picture to the specified folder. After taking photos, you need to notify the album to refresh.

Photo code

// Photo storage path    File fileDir = new File((), "Pictures");
    if (!()) {
      ();
    }

 //Picture name    String fileName = "/IMG_" + () + ".jpg";
    //Jump to camera    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    Uri uri;
    //Adapted to Android N version    if (.SDK_INT >= 24) {
      uri = (this, (this), new File(mFilePath));
    } else {
    //Before adapted to Android N version      uri = (new File(mFilePath));
    }
    (MediaStore.EXTRA_OUTPUT, uri);
    //Jump, need to be processed in onActivityResult    startActivityForResult(intent, 1111);

Refresh the album code

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

The above is the common photo code in Android Q before the version of Android Q. You can copy it directly if you need it.

Save the image to the sandbox

  • No permission is required to apply for operation files in the sandbox
  • Create a new folder in the sandbox only in the subfolder specified by the system.

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

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

This does not require calling the code to refresh the album library, because it cannot be displayed even after refreshing 😁

After careful comparison, I found that there is basically no difference between these two, only the file storage path is different, because the storage changes have been made on the new version of Android Q.

Save the image to a public folder

  //-------------------------
    //Photo storage path    //In order to adapt to Android Q version below    File fileDir = new File((), "Pictures");
    if (!()) {
      ();
    }

    String fileName = "/IMG_" + () + ".jpg";
    //--------------------------
    //--------------------------
    //Set parameters    Uri uri = null;
// Set save parameters to ContentValues    ContentValues contentValues = new ContentValues();
    //Set the file name    (.DISPLAY_NAME, fileName);
    //Compatible with Android Q and the following versions    if (.SDK_INT >= Build.VERSION_CODES.Q) {
      //The DATA field is no longer used in Android Q, but RELATIVE_PATH is used instead      //RELATIVE_PATH is a relative path, not an absolute path      //DCIM is a system folder. For the system folder, you can view it in the file manager that comes with the system. You cannot write unexisting names.      (.RELATIVE_PATH, "DCIM/Pictures");
    } else {
    //Android Q following version      (, mFilePath);
    }
    //Set file type    (.MIME_TYPE, "image/JPEG");
    //Execute the insert operation and add files to the system folder    //EXTERNAL_CONTENT_URI represents external memory, the value remains unchanged    uri = getContentResolver().insert(.EXTERNAL_CONTENT_URI, contentValues);
// If uri is generated, it means that the file is added successfully    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    (MediaStore.EXTRA_OUTPUT, uri);
    startActivityForResult(intent, 1111);

You don't need to refresh the album library when using this code, it will be refreshed automatically.
The operation of saving to a public file is relatively troublesome, but it's okay.

This is the article about how to implement photos and read albums with Android Q. This is the end. For more related contents of Android Q. Please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!