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;
    }
相关推荐
JFSJHFZJ3 小时前
清理手机顽固缓存,轻松释放几GB空间
缓存·智能手机
七夜zippoe17 小时前
脉向AI|当豆包手机遭遇“全网封杀“:GUI Agent是通向AGI的必经之路吗?
人工智能·ai·智能手机·agent·gui
crmscs17 小时前
剪映永久解锁版/电脑版永久会员VIP/安卓SVIP手机永久版下载
android·智能手机·电脑
lichensun17 小时前
2026美赛A题智能手机电池耗电建模—思路全解析(含具体模型和代码)
数学建模·智能手机
IT技术分享社区17 小时前
手机投屏到电视上,4种不同的方法,一起来了解一下
智能手机·投屏·手机技巧
巴德鸟17 小时前
华为手机鸿蒙4回退到鸿蒙3到鸿蒙2再回退到EMUI11 最后关闭系统更新
华为·智能手机·harmonyos·降级·升级·回退·emui
GR23423417 小时前
2025年影视仓TV+手机官方版 内置地址源支持高清直播
java·智能手机·软件
灿灿数模(加更版)17 小时前
2026美赛 MCM Problem A 智能手机电池耗电建模思路分析,完整论文
智能手机
普通网友17 小时前
苹果笔记本(Mac)连接手机完全指南
macos·智能手机
雾江流1 天前
小米堆叠桌面6.01.05.1991 | 提前享受丝滑堆叠桌面,仅限小米手机
智能手机·软件工程