Adapt to android4.4 ~ android11, call system camera, system album, system picture clipping, convert file (upload pictures, etc.)

preface

Recently, Android has modified many methods for files, and there are no adaptation schemes for system cameras, system albums and system tailoring from Android 4 to Android 11 on the network. I spent a few days summarizing the events and listing the source code first

Demo source code

First, make a preliminary summary of the Android file system:

Before Android Q (Android 10), Android's file system was not particularly strict. Each app can get the path of files in various locations, and the security was very poor.

After Android Q, the file system has been reformed, using the partitioned storage mode, also known as sandbox mode. What is sandbox? After each app is installed, a folder named after the app package will be created in the file system. This folder is called sandbox. In this mode, the application can only access the files inside the sandbox and the multimedia files and download files in the public directory.

Uri is required for photographing, selecting system album and cutting. URI is divided into two types: file type and content type. The URI of file type can directly obtain the real path of the URI. The URI of content type is an anonymous URI and cannot obtain the specific file path.

For androidq, the public directory is used uniformly for photographing and cutting pictures. For androidq and below, androidn (android7) needs to be distinguished. The URI used for photographing from androidn to androidq becomes content. If you still use the URI of file type, an error will be reported, so you need to use fileprovider for conversion, See the following adaptation process for details:

The pictures obtained by photographing and cutting will certainly be affected. The following is a basic introduction to adaptation.

Adaptation introduction

Add the following configuration in androidmanifest.xml:

Create an XML directory in the res folder of the project, and create a file in the XML directory_ Paths.xml file:

Define a global URI in the activity to receive pictures for subsequent operations:

1. Take photos

Check permissions:

Turn on the camera, and the URI here is the picture taken:

Specific logic:

Use the setimageuri () method of ImageView in onactivityresult to open the picture and tell the gallery to update the picture:

Because the broadcast update method has been deprecated:

Update the gallery using the following methods:

2. Photo album

Check permissions and open Album:

Album callback:

3. Cutting

Check permissions and open clipping:

Specific logic:

Crop callback:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>