android--调用系统相机和录像

一:照相:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button takePhoto;
    private ImageView showPhoto;

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

    }

    private void initView() {
        takePhoto = (Button) findViewById(R.id.takePhoto);
        showPhoto = (ImageView) findViewById(R.id.showPhoto);

        takePhoto.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.takePhoto:
                Intent intent = new Intent();
                intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent,100);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Bitmap bitmap = (Bitmap) data.getExtras().get("data");
        showPhoto.setImageBitmap(bitmap);

    }
}

二:录像

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button recard;
    private VideoView video;

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



    }

    private void initView() {
        recard = (Button) findViewById(R.id.recard);
        video = (VideoView) findViewById(R.id.video);

        recard.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.recard:
                Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
                startActivityForResult(intent,105);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == 105 && resultCode== Activity.RESULT_OK){

            Uri uri = data.getData();
            ContentResolver resolver = getContentResolver();
            Cursor cursor = resolver.query(uri, null, null, null, null);
            while(cursor.moveToFirst()){
                String path = cursor.getString(cursor.getColumnIndex("_data"));
                video.setVideoURI(Uri.parse(path));
                video.setMediaController(new MediaController(MainActivity.this));
                video.start();
            }
        }
    }
}

 

你可能感兴趣的:(android--调用系统相机和录像)