This Blog record from the album to choose a photo or take a photo to achieve the change of head and upload functions
Function realization is divided into two parts. The first part records the choice of photos and photos, the second part records the realization of upload function
Let’s start with the renderings
Here is the realization of album selection and camera photography
1. Choose pictures for album
Album selection is easy, just pull it up with the Intent setting
- Intent pulls up the album
*/ private void openGallery(int type) {Intent gallery = new Intent(Intent.ACTION_PICK); gallery.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); startActivityForResult(gallery, type); }Copy the code
- OnActivityResult Callback processing
@Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); // There is no check for a match, The data is empty Glide.with(this).load(data.getData()).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE).into(helpBinding.i vHelpImageFirst); }Copy the code
With just a few lines of code, you can pull up your album and select an image, and now you can take a picture
Two, the camera takes pictures
The preparatory work
We need to create a FileProvider first because we need to read the storage to store the photo. The reason why we use FileProvider instead of File is because, after Android N 7.0, in order to ensure the security of transferring files between apps, No longer allowed to pass file:// directly, which would Crash and throw FileUriExposedException, so grant temporary access via URI. (Ps: May not understand in place, in-depth understanding can be queried, here is a simple explanation)
So, before we start writing about cameras taking pictures, let’s take a look at FileProvider.
FileProvider
FileProvider is also relatively easy to use. There are three steps.
- Androidmanifest.xml defines the FileProvider
- Create an XML file that describes the path to temporarily grant permissions
- Using FileProvider
The following is a brief explanation
The first step:
<! Provider --> < Provider android: Android: authorities = "your package name. Fileprovider" android: exported = "false" android: grantUriPermissions = "true" > < meta - data android: android:resource="@xml/file_paths" /> </provider>Copy the code
Name: FileProvider is derived from ContentProvider, and is now available on androidx. Exported: Whether to disclose grantUriPermissions: Whether to grant temporary permissions
Then there is meta-data, since we can’t use File directly, but URI, we need the configuration File to map the actual storage location. This resource is our mapping configuration file, which paths are authorized to write in this file.
The second step:
Create an XML folder in the RES directory and create the file_paths.xml file. Location: res/XML/file_paths. XML
<? The XML version = "1.0" encoding = "utf-8"? > <paths> <external-path /> </paths>Copy the code
Since I’m lazy, I’ll just authorize it from the root directory, using.
Step 3:
And then you use it in your code
/ / directly by this method can get the URI URI URI = FileProvider. GetUriForFile (this, "in the AndroidManifest. The identity of the" declaration in the XML, file));Copy the code
OK, after a brief introduction to FileProvider, move on.
3. Camera text
- Create a photo storage path
- Pull up the camera and take a picture
- OnActivityResult Callback processing
The first step:
We need a path to store the photos we take
ImgDir = new File(screenUtils.getFilepath (this, null)); // Screenutils.getFilepath (screenutils.getFilepath); String photoName = System.currentTimemillis () + ".png"; File picture = new File(imgDir, photoName); if (! picture.exists()) { try { picture.createNewFile(); } catch (IOException e) {log.e (TAG, "choosePictureTypeDialog: failed to create image ", e); ImgPath = picture.getabsolutePath (); // imgPath = picture.getabsolutePath ();Copy the code
The second step:
Create the Intent to pull up the camera
Intent camera = new Intent(mediastore.action_image_capture); Camera. PutExtra (MediaStore. EXTRA_OUTPUT, FileProvider getUriForFile (this, "declared in AndroidManifest FileProvider logo", picture)); startActivityForResult(camera, type);Copy the code
Step 3:
Callback logic processing
@Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); // The camera callback data is null, the album callback is not null, Glide.with(this).load(imgPath).skipMemoryCache(true).diskCacheStrategy(diskCacheStrategy.None).into(imageView); }Copy the code
At this point, you are done selecting images from the album and taking photos from the camera.
Complete code GitHub address
The next Blog to achieve the upload function.
The following is for reference: Android N FileProvider Description How to use FileProvider