I won’t say much nonsense, I will just post the code to you. The specific code is as follows:
/** * 1. Implementation principle: After the user opens the album or selects the photo, the photo is compressed and set on the control. The picture is stored on the local SD card (if there is, it will be stored internally, so it is also * You need to determine whether the user has mounted the SD card), and then store a copy of the image on the server. When the application is started again next time, the SD card will be loaded by default. If there is no local area, you will go to the Internet to request it. * 2. Use the picasso framework and custom BitmapUtils tool class * 3. Remember to add relevant permissions * <uses-permission android:name=""></uses-permission> <uses-permission android:name=""/> <uses-permission android:name=".WRITE_EXTERNAL_STORAGE"></uses-permission> * */ public class MainActivity extends AppCompatActivity implements { private ImageView iv;//The avatar to be set private Button btn_photo;//Calling the album button private Button btn_camera;//Calling the camera button @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); iv=(ImageView) findViewById(); btn_photo = (Button) findViewById(.btn_photo); btn_camera = (Button) findViewById(.btn_camera); btn_photo.setOnClickListener(this); btn_camera.setOnClickListener(this); } @Override public void onClick(View v) { switch (()) { case .btn_photo://Open the system album Intent intent=new Intent(Intent.ACTION_PICK, .EXTERNAL_CONTENT_URI); startActivityForResult(intent,100); break; case .btn_camera://Open the system camera Intent intent2=new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent2,200); break; } } @RequiresApi(api = Build.VERSION_CODES.KITKAT) @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { (requestCode, resultCode, data); if(requestCode==100&&resultCode==RESULT_OK&&data!=null){//System photo album Uri imageData = (); String path=getPath(imageData); Bitmap bitmap = (path); Bitmap bitmap1 = (bitmap, (), ()); Bitmap bitmap2 = (bitmap1); //Loading display (bitmap2); // Upload bitmap pictures to the server... //Save the bitmap image locally saveImage(bitmap2); }else if(requestCode==200&&resultCode==RESULT_OK&&data!=null){//System Camera Bitmap bitmap = (Bitmap) ().get("data"); (bitmap,(),()); bitmap=(bitmap); //Loading display (bitmap); // Upload bitmap pictures to the server... //Save the bitmap image locally saveImage(bitmap); } } /** * Storage of data. (5 types) * Bimap: memory-level image object. * * Storage --->Memory: * (String filePath); * (InputStream is); * Memory --->Storage: * (,100,OutputStream os); */ private void saveImage(Bitmap bitmap) { File filesDir; if(().equals(Environment.MEDIA_MOUNTED)){//Judge whether the SD card is mounted //Path 1: storage/sdcard/Android/data/package name/files filesDir = (""); }else{//Internal storage of the mobile phone //Path: data/data/package name/files filesDir = (); } FileOutputStream fos = null; try { File file = new File(filesDir,""); fos = new FileOutputStream(file); (, 100,fos); } catch (FileNotFoundException e) { (); }finally{ if(fos != null){ try { (); } catch (IOException e) { (); } } } } //If there is locally, there is no need to go online to request it again private boolean readImage() { File filesDir; if(().equals(Environment.MEDIA_MOUNTED)){//Judge whether the SD card is mounted //Path 1: storage/sdcard/Android/data/package name/files filesDir = getExternalFilesDir(""); }else{//Internal storage of the mobile phone //Path: data/data/package name/files filesDir = getFilesDir(); } File file = new File(filesDir,""); if(()){ //Storage--->Memory Bitmap bitmap = (()); (bitmap); return true; } return false; } @RequiresApi(api = Build.VERSION_CODES.KITKAT) private String getPath(Uri uri) { int sdkVersion = .SDK_INT; // Versions higher than 4.4.2 if (sdkVersion >= 19) { ("TAG", "uri auth: " + ()); if (isExternalStorageDocument(uri)) { String docId = (uri); String[] split = (":"); String type = split[0]; if ("primary".equalsIgnoreCase(type)) { return () + "/" + split[1]; } } else if (isDownloadsDocument(uri)) { final String id = (uri); final Uri contentUri = (("content://downloads/public_downloads"), (id)); return getDataColumn(this, contentUri, null, null); } else if (isMediaDocument(uri)) { final String docId = (uri); final String[] split = (":"); final String type = split[0]; Uri contentUri = null; if ("image".equals(type)) { contentUri = .EXTERNAL_CONTENT_URI; } else if ("video".equals(type)) { contentUri = .EXTERNAL_CONTENT_URI; } else if ("audio".equals(type)) { contentUri = .EXTERNAL_CONTENT_URI; } final String selection = "_id=?"; final String[] selectionArgs = new String[]{split[1]}; return getDataColumn(this, contentUri, selection, selectionArgs); } else if (isMedia(uri)) { String[] proj = {}; Cursor actualimagecursor = (uri, proj, null, null, null); int actual_image_column_index = (); (); return (actual_image_column_index); } } else if ("content".equalsIgnoreCase(())) { // Return the remote address if (isGooglePhotosUri(uri)) return (); return getDataColumn(this, uri, null, null); } // File else if ("file".equalsIgnoreCase(())) { return (); } return null; } /** * Uri path query field * * @param context * @param uri * @param selection * @param selectionArgs * @return */ public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) { Cursor cursor = null; final String column = "_data"; final String[] projection = {column}; try { cursor = ().query(uri, projection, selection, selectionArgs, null); if (cursor != null && ()) { final int index = (column); return (index); } } finally { if (cursor != null) (); } return null; } private boolean isExternalStorageDocument(Uri uri) { return "".equals(()); } public static boolean isDownloadsDocument(Uri uri) { return "".equals(()); } public static boolean isMediaDocument(Uri uri) { return "".equals(()); } public static boolean isMedia(Uri uri) { return "media".equals(()); } /** * @param uri The Uri to check. * @return Whether the Uri authority is Google Photos. */ public static boolean isGooglePhotosUri(Uri uri) { return "".equals(()); } /** * To determine whether there is this image locally, if there is no one, go to the Internet to request it * */ @Override protected void onResume() { (); if(readImage()){ return; } } } //BitmapUtils tool class public class BitmapUtils { /** * This method is used to circle the image * */ public static Bitmap circleBitmap(Bitmap source){ //By default, only width is processed int width=(); Bitmap bitmap=(width,width,.ARGB_8888); Canvas canvas=new Canvas(bitmap); Paint paint=new Paint(); //Set up anti-aliasing (true); (width/2,width/2,width/2,paint); (new PorterDuffXfermode(.SRC_IN)); (source,0,0,paint); return bitmap; } /** * This method is used for image compression processing. Note that the type of width and height parameters must be float * */ public static Bitmap zoom(Bitmap source,float width,float height){ Matrix matrix=new Matrix(); //Image compression process (width/(),height/()); Bitmap bitmap = (source, 0, 0, (), (), matrix, false); return bitmap; }}
The above is the Android implementation call system gallery and camera setting avatar introduced by the editor to you. I hope it will be helpful to you. If you have any questions, please leave me a message. The editor will reply to you in time!