Android uses the framework to implement two lines of code to change the avatar from a local picture or taking a photo?

Still the same, it is recommended that you create a new page or project yourself when testing, to avoid other factors that may cause the results to fail.
As the so-called predecessors planted trees, future generations enjoy the cool . I belong to the one who enjoys the shade.
First, I wrote the official website address of the dependency: PictureSelector

Step 1: Import dependencies

code show as below:

implementation 'com.github.wildma:PictureSelector:2.1.0'

Add this dependency to your project, here I am creating a new test6 to test, so I put it in test6, you put it in the build.gradle file in the project you need.
Insert picture description here
After importing dependencies

Step 2: Configure the xml file for testing.

Here I use an ImageView and a Button component to test.
As shown in the figure: the
Insert picture description here
code is relatively simple, you can also write it yourself:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:src="@drawable/ic_launcher_background"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:text="点击更改图片"
        app:layout_constraintEnd_toEndOf="@+id/imageView"
        app:layout_constraintStart_toStartOf="@+id/imageView"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />
</androidx.constraintlayout.widget.ConstraintLayout>

The third step: write Activity code.

code show as below:

package com.example.test6;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import com.wildma.pictureselector.PictureBean;
import com.wildma.pictureselector.PictureSelector;

public class MainActivity extends AppCompatActivity {
    
    
    private PictureBean pictureBean;
    private ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView=findViewById(R.id.imageView);
    }

    public void paizhao(View v){
    
    
        PictureSelector
                .create(MainActivity.this, PictureSelector.SELECT_REQUEST_CODE)
                .selectPicture(true);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
    
        super.onActivityResult(requestCode, resultCode, data);
        /*结果回调*/
        if (requestCode == PictureSelector.SELECT_REQUEST_CODE) {
    
    
            if (data != null) {
    
    
                pictureBean = data.getParcelableExtra(PictureSelector.PICTURE_RESULT);
                if (pictureBean.isCut()) {
    
    
                    imageView.setImageBitmap(BitmapFactory.decodeFile(pictureBean.getPath()));
                } else {
    
    
                    imageView.setImageURI(pictureBean.getUri());
                }
            }
        }
    }
}

The following is the analysis of the code:
Insert picture description here
Then, here is very important:
because we have just written the method in the red circle below, we must call it to respond to this method.
Insert picture description here
Call this method through our just Button. As shown in the figure,
Insert picture description here
we can see that the method just changed from gray to yellow, indicating that the call was successful:
Insert picture description here

Finally, click Run to view the results.

First, let’s take a look
Insert picture description here
at what it looks like when the picture is not modified: then look at the process and agree to his permission requirements. Then we can see that the picture has been modified successfully. Just take a picture and try it yourself. It’s already possible.

Insert picture description here
Insert picture description here
Insert picture description here
This completes the functions we need.

Guess you like

Origin blog.csdn.net/qq_45137584/article/details/110913393