Android7.0启用摄像头拍照

public void takePhone(){
    //启动拍照的Activity
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    String path = getExternalCacheDir()+"haha.jpg";
    Uri uri = null;
    //android7.0使用
    if (Build.VERSION.SDK_INT >= 24){
        uri = FileProvider.getUriForFile(this,"com.example.camerallbumtest.fileprovider", new File(path));
    } else {
        uri = Uri.fromFile(new File(path));
    }
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
    startActivityForResult(intent, 1);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == 1){
        if (resultCode == RESULT_OK){
            Toast.makeText(this, "保存照片", Toast.LENGTH_SHORT).show();

            try {
                Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(Uri.fromFile(new File(getExternalCacheDir(),"haha.jpg"))));
                ImageView iv = (ImageView) findViewById(R.id.iv);
                iv.setImageBitmap(bitmap);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        } else {
            Toast.makeText(this, "没保存照片", Toast.LENGTH_SHORT).show();
        }
    }

}

你可能感兴趣的:(Android基础)