Android相机拍摄并保存
创始人
2024-10-12 12:30:33
0

下面是一个示例代码,展示了如何在Android设备上使用相机拍摄照片并保存到本地存储。

首先,确保你的Android项目中已经添加了相机权限:



然后,创建一个包含相机功能的Activity,例如CameraActivity:

public class CameraActivity extends AppCompatActivity {

    private static final int REQUEST_IMAGE_CAPTURE = 1;

    private ImageView imageView;
    private String currentPhotoPath;

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

        imageView = findViewById(R.id.imageView);

        Button captureButton = findViewById(R.id.captureButton);
        captureButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dispatchTakePictureIntent();
            }
        });
    }

    private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(this,
                        "com.example.android.fileprovider",
                        photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
            }
        }
    }

    private File createImageFile() throws IOException {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,
                ".jpg",
                storageDir
        );
        currentPhotoPath = image.getAbsolutePath();
        return image;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            galleryAddPic();
            setPic();
        }
    }

    private void galleryAddPic() {
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File f = new File(currentPhotoPath);
        Uri contentUri = Uri.fromFile(f);
        mediaScanIntent.setData(contentUri);
        this.sendBroadcast(mediaScanIntent);
    }

    private void setPic() {
        int targetW = imageView.getWidth();
        int targetH = imageView.getHeight();

        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(currentPhotoPath, bmOptions);
        int photoW = bmOptions.outWidth;
        int photoH = bmOptions.outHeight;

        int scaleFactor = Math.min(photoW / targetW, photoH / targetH);

        bmOptions.inJustDecodeBounds = false;
        bmOptions.inSampleSize = scaleFactor;
        bmOptions.inPurgeable = true;

        Bitmap bitmap = BitmapFactory.decodeFile(currentPhotoPath, bmOptions);
        imageView.setImageBitmap(bitmap);
    }
}

在上面的代码中,我们创建了一个CameraActivity,并在布局文件中添加了一个ImageView和一个用于触发拍照的按钮。当用户点击拍照按钮时,我们调用dispatchTakePictureIntent()方法来启动相机应用,并将拍摄的照片保存到一个临时文件中。

在createImageFile()方法中,我们创建了一个以时间戳命名的临时文件,并将其保存到应用的外部存储目录下的Pictures文件夹中。

在onActivityResult()方法中,我们调用galleryAddPic()方法将照片添加到Android的媒体库中,并调用setPic()方法将照片显示在ImageView中。

请注意,上面的代码中使用了FileProvider来获取拍摄照片的URI,并且需要在Manifest文件中添加FileProvider的配置:


    

在res/xml文件夹中创建一个名

相关内容

热门资讯

总算清楚!wepoker祈福有... 总算清楚!wepoker祈福有用吗,wepoker私人局怎么玩,攻略教程(有挂方法);玩家必备必赢加...
记者揭秘!aapoker辅助怎... 记者揭秘!aapoker辅助怎么用,hhpoker是真的还是假的,专业教程(有挂技巧);hhpoke...
盘点一款!epoker透视底牌... 1、盘点一款!epoker透视底牌,大菠萝免费辅助,详细教程(有挂辅助);详细教程。2、大菠萝免费辅...
重大通报!wepoker高级辅... 1、重大通报!wepoker高级辅助,wepoker国外版透视,规律教程(有挂技巧);详细教程。2、...
一秒答解!wepoker究竟有... 一秒答解!wepoker究竟有没有透视,hhpoker脚本下载,介绍教程(有挂透明);建议优先通过w...
每日必备!aapoker公共底... 1、每日必备!aapoker公共底牌,wepoker底牌透视脚本,攻略方法(有挂软件)(UU pok...
今日百科!wepoker破解游... 今日百科!wepoker破解游戏盒子,hardrock透视工具,新2025教程(有挂软件)是由北京得...
1.9分钟了解!wepoker... 自定义wepoker私人局俱乐部辅助系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专...
程序员教你!aapoker辅助... 自定义hhpoker视频巡查真的假的系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专...
盘点十款!购买wepoker模... 这是一款非常优秀的aapoker如何设置胜率 ia辅助检测软件,能够让你了解到aapoker如何设置...