android 调用系统相机拍照并保存照片原图

1,在清单文件里设置拍照权限


    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
     
    <uses-permission android:name="android.permission.RECORD_AUDIO" />

2,跳转到系统相机的拍照界面或者录像界面


private static final int CAMERA = 0;
private static final int CASE_VIDEO = 1;
----------------------------------------------------------
//拍照
photoDir = new File(MyConstants.PHOTO_PATH);
            if (!photoDir.exists()) {
                photoDir.mkdirs();
            }
            String name = "temp.jpg";
            photo = new File(photoDir, name);
            Uri photoUri = Uri.fromFile(photo);
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
            startActivityForResult(intent, CAMERA);
//录像
Intent intent1 = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
            intent1.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
            startActivityForResult(intent1, CASE_VIDEO);
            SimpleDateFormat formatter = new SimpleDateFormat(
                    "yyyy年MM月dd日    HH:mm:ss     ");
            Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
            date1 = formatter.format(curDate);

3,重写onActivityResult()方法,根据拍照或者录像成功的返回值,做相应的操作

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CAMERA && resultCode == Activity.RESULT_OK) {
            if (photo.exists()) {
                String name = new DateFormat().format("yyyyMMdd_hhmmss",
                        Calendar.getInstance(Locale.CHINA)) + ".jpg";
                File newFile = new File(photoDir, name);
                photo.renameTo(newFile);
                String fileSize = FileSizeUtil.getAutoFileOrFilesSize(newFile
                        .getAbsolutePath());
                String date = new DateFormat().format("yyyyMMddhh:mm:ss",
                        Calendar.getInstance(Locale.CHINA)).toString();

            }
        }
        if (requestCode == CASE_VIDEO && resultCode == Activity.RESULT_OK
                && null != data) {
            Uri uri = data.getData();
            String filePath = "";
            if (uri == null) {
                return;
            } else {
                Cursor c = getActivity().getContentResolver().query(uri,
                        new String[] { MediaStore.MediaColumns.DATA }, null,
                        null, null);
                if (c != null && c.moveToFirst()) {
                    filePath = c.getString(0);
                }
            }


        }

4,根据文件路径获取文件大小的方法

public static double getAutoFileSize(String filePath) {
        File file = new File(filePath);
        long blockSize = 0;
        try {
            if (file.isDirectory()) {
                blockSize = getFileSizes(file);
            } else {
                blockSize = getFileSize(file);
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("获取文件大小", "获取失败!");
        }
        return FormatFileSize(blockSize);
    }

你可能感兴趣的:(android)