嵌入式Android开发:从相册中获取图片(最新版API)

介绍

最新版API(30)改了很多,去掉了常用的onActivityResult,因此获取图片方式也有了改变。

代码

代码如下:

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)
    {
    
    //拍照片

    }
}

对于从相册中获取的返回值,不同的类型的打印语句打印的信息如下:
打印语句:

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
                        }
                    }
                }
            }
    );

打印的结果为:
在这里插入图片描述
第一条语句是直接把intent转为了string,因此其string结果开头带了intent
第二条语句得到是把intent.getData打印出来,其类型为uri转string
第三条语句是uri转string,因此第二条、第三条得到的结果一样。

效果

在这里插入图片描述
点击后
在这里插入图片描述

选择图片后
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/wcc243588569/article/details/129896115