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;
    }
相关推荐
花先锋队长11 小时前
华为Mate X7:高级感,从何而来?
科技·华为·智能手机·harmonyos
wanhengidc11 小时前
G口服务器具体是指什么
运维·服务器·游戏·智能手机
wanhengidc11 小时前
云手机面向的用户群体都有哪些?
运维·服务器·科技·智能手机·云计算
思茂信息12 小时前
CST软件对Customer Success OPPO手机电源适配器EMC仿真
开发语言·嵌入式硬件·matlab·3d·智能手机·cst
xiaocao_102312 小时前
在鸿蒙手机上有哪些比较高效的待办清单记事软件?
华为·智能手机·harmonyos
wanhengidc12 小时前
云手机 黑科技技术 巨 椰
科技·智能手机
wanhengidc14 小时前
云计算时代 云手机与云服务器的不同
服务器·智能手机·云计算
2501_915918411 天前
iOS 手机抓包软件怎么选?HTTPS 调试、TCP 数据流分析与多工具组合的完整实践
android·ios·智能手机·小程序·https·uni-app·iphone
brave and determined3 天前
接口通讯学习(day05):智能手机的内部高速公路:揭秘MIPI CSI与DSI技术
学习·智能手机·软件工程·制造·csi·mipi·dsi
解压专家6664 天前
KRed阅读器用二维码重塑电子书分享体验
智能手机