Embedded Android development: Get pictures from the photo album (latest version API)

introduce

The latest version of API (30) has changed a lot, and the commonly used ones have been removed onActivityResult, so the way to obtain pictures has also changed.

the code

code show as below:

package com.example.myapplication;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class CameraTestActivity extends AppCompatActivity {
    
    

    ImageView ivChoose,ivTake;
    Button btn_c,btn_t;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera_test);

        ivChoose = findViewById(R.id.imageViewChoice);
        ivTake = findViewById(R.id.imageViewTake);
        btn_c = findViewById(R.id.buttonChoose);
        btn_t = findViewById(R.id.buttonTakePhoto);

        btn_c.setOnClickListener(this::btnClickChoose);
        btn_t.setOnClickListener(this::btnClickTake);
    }

    ActivityResultLauncher ChooseImg = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            new ActivityResultCallback<ActivityResult>() {
    
    
                @Override
                public void onActivityResult(ActivityResult result) {
    
    
                    if(result.getResultCode() == RESULT_OK)
                    {
    
    
                        if(result.getData() != null)
                        {
    
    
                            String im = result.getData().toString();
                            //result是ActivityResult类型,result.getData()是intent类型,result.getData().getData()是Uri类型
                            Uri imuri = result.getData().getData();
                            //得到的是:Intent { dat=content://com.android.providers.media.documents/document/image:197731 flg=0x1 }
                            //Bitmap bim = B
                            ivChoose.setImageURI(imuri);
                            Log.d("img uri",im);
                            Log.d("Get img uri",result.getData().getDataString());
                            Log.d("img uri",imuri.toString());

//                            ivChoose.setimage
                        }
                    }
                }
            }
    );
//    //参考官网:https://developer.android.com/training/data-storage/shared/photopicker?hl=zh-cn#java
//    ActivityResultLauncher ChooseImg = registerForActivityResult(
//            new PickVisualMedia
//
//    );

    public void btnClickChoose(View v)
    {
    
    //选择照片
        Intent Album = new Intent(Intent.ACTION_GET_CONTENT);
        //ACTION_GET_CONTENT是获取本地所有图片,ACTION_PICK获取的是相册中的图片
        Album.setType("image/*");
        //设置图像类型
        ChooseImg.launch(Album);

    }

    public void btnClickTake(View v)
    {
    
    //拍照片

    }
}

For the return value obtained from the photo album, the information printed by different types of print statements is as follows:
print statement:

ActivityResultLauncher ChooseImg = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            new ActivityResultCallback<ActivityResult>() {
    
    
                @Override
                public void onActivityResult(ActivityResult result) {
    
    
                    if(result.getResultCode() == RESULT_OK)
                    {
    
    
                        if(result.getData() != null)
                        {
    
    
                            String im = result.getData().toString();
                            //result是ActivityResult类型,result.getData()是intent类型,result.getData().getData()是Uri类型
                            Uri imuri = result.getData().getData();
                            //得到的是:Intent { dat=content://com.android.providers.media.documents/document/image:197731 flg=0x1 }
                            //Bitmap bim = B
                            ivChoose.setImageURI(imuri);
                            Log.d("img uri",im);
                            Log.d("Get img uri",result.getData().getDataString());
                            Log.d("img uri",imuri.toString());

//                            ivChoose.setimage
                        }
                    }
                }
            }
    );

The printed result is:
insert image description here
the first statement directly converts the intent into a string, so the string result starts with intent. The
second statement is to print out intent.getData, and its type is uri to string.
The third statement is uri Convert string, so the second and third results are the same.

Effect

insert image description here
after click
insert image description here

After selecting an image
insert image description here

Guess you like

Origin blog.csdn.net/wcc243588569/article/details/129896115
Recommended