First, picture selection

1.1 the target

1. Realize the function as shown in the picture: album and camera options can appear

2. Able to crop the selected picture

1.2 Function Implementation

Encapsulates the tool class for selecting and clipping images

Public static Intent getPhotoSelectIntent(uri uri){Intent take = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); take.addCategory(Intent.CATEGORY_DEFAULT); take.putExtra(MediaStore.EXTRA_OUTPUT, uri); Intent pics = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); Intent = Intent. CreateChooser (pics," select image "); chose.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Parcelable[]{take}); return chose; } /** ** picture clipping * @param inputUri clipping picture * @param outputUri clipping location * @param width clipping width * @param height clipping height * @return */ public static Intent getImageCropIntent(Uri inputUri, Uri outputUri, int width, int height) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(inputUri, "image/*"); PutExtra ("crop", "true"); // Crop =true. intent.putExtra("scale", true); Intent. PutExtra ("scaleUpIfNeeded", true); // Intent. Intent. PutExtra ("aspectX", width); Intent. PutExtra ("aspectY", height); PutExtra ("outputX", width); // Intent.putextra ("outputX", width); Intent. PutExtra ("outputY", height); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri); intent.putExtra("return-data", false); // Set the intent to not return data. }Copy the code

1.2.2 Add click image selection event

b.ivAvatar.setOnClickListener {
   mTakePhotoFile = File(getPicPath() + File.separator + System.currentTimeMillis() + ".jpeg")
   val uri = Uri.fromFile(mTakePhotoFile)
   startActivityForResult(IntentUtils.getPhotoSelectIntent( uri), TAKE_PHOTO_REQ)
  }
Copy the code

Image clipping requires a Uri of the form: content://, so a utility class to get the Content Uri needs to be packaged

public static Uri getContentUri(Context context, File file) { String filePath = file.getAbsolutePath(); Cursor cursor = context.getContentResolver().query( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[] { MediaStore.Images.Media._ID }, MediaStore.Images.Media.DATA + "=? ", new String[] { filePath }, null); if (cursor ! = null && cursor.moveToFirst()) { int id = cursor.getInt(cursor .getColumnIndex(MediaStore.MediaColumns._ID)); Uri baseUri = Uri.parse("content://media/external/images/media"); return Uri.withAppendedPath(baseUri, "" + id); } else { if (file.exists()) { ContentValues values = new ContentValues(); values.put(MediaStore.Images.Media.DATA, filePath); return context.getContentResolver().insert( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); } else { return null; }}}Copy the code

Processing feedback results

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) if(resultCode ! Separator = -1) {return} when (requestCode) {TAKE_PHOTO_REQ - {mCutPhotoFile = File(getPicPath() + file.separator + "avatar_" + System.currentTimeMillis() + "jpeg") val cutUri = Uri.fromFile(mCutPhotoFile) if (data ! = null){ startActivityForResult(IntentUtils.getImageCropIntent(data.data, cutUri, 200, 200), CUT_PHOTO_REQ) } else { val uri = UriUtils.getContentUri(applicationContext, mTakePhotoFile) startActivityForResult(IntentUtils.getImageCropIntent(uri, cutUri, 200, 200), CUT_PHOTO_REQ)}} CUT_PHOTO_REQ - {// Process image cropping results}}}Copy the code

1.2.4 Android 7.0 Adaptation

  1. Res/XML/PROVIDer_paths.xml Paths change themselves
<paths xmlns:android="http://schemas.android.com/apk/res/android" 
 <external-path path="Android/data/com/example/sunmoon/images" name="sdcard_files" / 
 <external-files-path path="Android/data/com/example/sunmoon/images" name="camera_has_sdcard"/ 
 <files-path path="Android/data/com/example/sunmoon/other"  name="camera_no_sdcard"/ 
 <external-path path="Android/data/com/example/sunmoon" name="files_root" / 
 <external-path path="." name="external_storage_root" / 
</paths 
Copy the code
  1. Do not use sse configuration package name
. <application <provider android:name="android.support.v4.content.FileProvider" android:authorities="com.example.sunmoon.provider" android:exported="false" android:grantUriPermissions="true" <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"/ </provider ... </application ...Copy the code

conclusion

The above is xiaobian to introduce the Android project actual combat avatar selection function, I hope to help you, if you have any questions please leave a message to me, xiaobian will reply to you in time.

Related video

【 B station in 2021, the most complete series of Android project combat 】10 Android project collection strongly recommend collection ~ advanced UI/ smart koi/performance optimization/audio and video development combat to learn this set of direct into the factory