启动系统Camera拍照(Android)

代码功能说明:启动手机自带camera应用,拍照后回到对应应用


    private static final int TAKE_PICTURE = 1;
    private String TEMP_FILE = "";

    public String buildFileName(){
        Date now = new Date();
        SimpleDateFormat formater = new SimpleDateFormat("yyMMdd-HHmmss");

        String SDCARD = Environment.getExternalStorageDirectory() + "/";
        String basePath = SDCARD + "Pictures/";

        File dir = new File(basePath);
        dir.mkdirs();

        return basePath + formater.format(now) + ".jpg";
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        String filename = "";
        try {
            if (resultCode == Activity.RESULT_OK && requestCode == TAKE_PICTURE) {
                File file = new File(TEMP_FILE);
                if(file.exists()){
                    filename = file.getPath();
                    Toast.makeText(MainActivity.this, "图片已保存在:" + filename, Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(MainActivity.this, "图片保存失败", Toast.LENGTH_LONG).show();
                }
            }
        } catch (Exception err) {
            Log.e("Exception", err.getLocalizedMessage());
        }
    }
    /**
     * 拍照
     */
    public void doTakePicture(){
        TEMP_FILE = buildFileName();
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //调用摄像头拍照
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(TEMP_FILE))); //另存图片地址
        startActivityForResult(intent, TAKE_PICTURE);
    }

你可能感兴趣的:(Android)