Bootstrap

android 手机拍照相册选照片简单实现

效果

1.先看清单文件配置
     <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.yourpackage.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />

对应的清单文件权限 相机 文件读写
   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA" />

2.在res 文件下创建file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>

    <external-path
        name="external"
        path="." />
    <external-files-path
        name="external_files"
        path="." />
    <cache-path
        name="cache"
        path="." />
    <external-cache-path
        name="external_cache"
        path="." />
    <files-path
        name="files"
        path="." />

</paths>
3.下面对应的code  activity /xml
 <ImageView
        android:id="@+id/img"
        android:layout_width="300dp"
        android:layout_height="100dp"
        android:src="@mipmap/ic_launcher"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:layout_marginTop="30dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        app:layout_constraintTop_toBottomOf="@+id/img"
        tools:ignore="NotSibling">

        <Button
            android:id="@+id/paizhao"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="拍照" />

        <Button

            android:id="@+id/xc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:text="相册"

            />

逻辑如下:
 Button paizhao, xc;
    ImageView img;
    private static final int REQUEST_IMAGE_CAPTURE = 1;
    private static final int SELECT_PHOTO = 2;

    private Uri imageUri;

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

        img = (ImageView) findViewById(R.id.img);
        paizhao = (Button) findViewById(R.id.paizhao);
        xc = (Button) findViewById(R.id.xc);

        // 拍照
        paizhao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                    // 创建临时文件用于存储拍摄的照片
                    File photoFile = createImageFile();
                    if (photoFile != null) {
                        imageUri = FileProvider.getUriForFile(FirmActivity.this,
                                "com.yourpackage.fileprovider",
                                photoFile);
                        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
                    }
                }
            }
        });
        // 从相册选择

        xc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                startActivityForResult(intent, SELECT_PHOTO);
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_IMAGE_CAPTURE) {
                // 处理拍照结果
                Uri takenPhotoUri = imageUri;
                Log.d("拍照uri:", "onActivityResult: "+takenPhotoUri);
                Glide.with(this).load(takenPhotoUri).into(img);
                // 在这里处理拍照的照片
            } else if (requestCode == SELECT_PHOTO) {
                // 处理选择照片结果
                Uri selectedPhotoUri = data.getData();
                // 在这里处理选择的照片
                Log.d("相册uri:", "onActivityResult: "+selectedPhotoUri);
                Glide.with(this).load(selectedPhotoUri).into(img);
            }
        }
    }


    private File createImageFile() {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = null;
        try {
            image = File.createTempFile(imageFileName, ".jpg", storageDir);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return image;
    }






;