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;
    }
相关推荐
Lancker1 天前
定制侠 一个国产纯血鸿蒙APP的诞生过程
android·华为·智能手机·鸿蒙·国产操作系统·纯血鸿蒙·华为鸿蒙
新诺韦尔API2 天前
手机三要素验证接口详细技术对接指南
大数据·智能手机·api
铁蛋AI编程实战2 天前
Falcon-H1-Tiny 微型 LLM 部署指南:100M 参数也能做复杂推理,树莓派 / 手机都能跑
java·人工智能·python·智能手机
AirDroid_cn2 天前
双机党:一台小米一台华为,怎样相互远程控制?
智能手机
玉梅小洋4 天前
手机 App 云端存储云服务选型指南
人工智能·智能手机·手机·工具开发·手机app开发
玉梅小洋4 天前
手机 App 跨平台框架统一目录构建
智能手机·手机·app开发
东哥笔迹4 天前
高通骁龙Android手机平台EIS基础pipeline(二)
智能手机
jian110585 天前
Android studio 调试flutter 运行自己的苹果手机上
flutter·智能手机·android studio
小锋学长生活大爆炸5 天前
【工具】手机控制iPixel LED屏实现转向和刹车联动、语音控制显示内容
智能手机·工具·led·车机·智能·diy·ipixel
Boxsc_midnight5 天前
【openclaw+imessage】【免费无限流量】集成方案,支持iphone手机+macos
ios·智能手机·iphone