Android打开系统相机或从相册中选择图片并显示

1.打开系统相机

Intent intent = new Intent(); //调用照相机 
intent.setAction("android.media.action.STILL_IMAGE_CAMERA"); 
startActivity(intent); 

 2.从相册中选择图片并显示

xml代码



    
    
        

展示

Android打开系统相机或从相册中选择图片并显示_第1张图片

 java代码


import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;


public class PhotoActivity extends AppCompatActivity {

    private Button btn_upload;
    private ImageView iv_image;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e(this.getClass().getName(), "onCreate");
        setContentView(R.layout.activity_photo);
        btn_upload = findViewById(R.id.btn_upload);
        iv_image = findViewById(R.id.iv_image);
        btn_upload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK, null);
                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                startActivityForResult(intent, 2);
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2) {
            // 从相册返回的数据
            Log.e(this.getClass().getName(), "Result:" + data.toString());
            if (data != null) {
                // 得到图片的全路径
                Uri uri = data.getData();
                iv_image.setImageURI(uri);
                Log.e(this.getClass().getName(), "Uri:" + String.valueOf(uri));
            }
        }
    }

}

实机演示

Android打开系统相机或从相册中选择图片并显示_第2张图片

 

你可能感兴趣的:(笔记,Android,android,android,studio,ide)