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;
    }
相关推荐
开开心心就好8 小时前
娱乐使用,可以生成转账、图片、聊天等对话内容
windows·python·智能手机·软件工程·娱乐·软件需求
农夫山泉2号19 小时前
【个人开源】——从零开始在高通手机上部署sd(一)
stable diffusion·智能手机·sd·高通·qnn
熹乐互动20 小时前
现场可以通过手机或者pad实时拍照上传到大屏幕的照片墙现场大屏电子照片墙功能
智能手机
2501_904447741 天前
苹果确认iOS 18.4四月初推出:Apple Intelligence将迎来中文支持
华为·智能手机·django·virtualenv·pygame
Hacker_Fuchen1 天前
android 网络防护 手机网络安全怎么防
网络·web安全·智能手机
沙漠蓝色披头3 天前
通过wifi无线方式,通过adb命令连接手机,用来传输文件和安装app
adb·智能手机
OJAC近屿智能3 天前
苹果新品今日发布,AI手机市场竞争加剧,近屿智能专注AI人才培养
大数据·人工智能·ai·智能手机·aigc·近屿智能
解压专家6663 天前
解压软件手机版推荐:手机端高效解压工具
智能手机·winrar·7-zip
阿7_QuQ3 天前
手机控制电脑远程关机
智能手机
greenery3 天前
旧手机热点无法提供ipv6解决方法(emui 8 热点提供ipv6)
网络·智能手机