android之拍照与摄像

拍照和摄像的意图很简答,这里直接贴代码

布局文件


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="拍照"
    android:onClick="image"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="摄像"
        android:onClick="video"/>
</LinearLayout>

Activity


apackage xidian.dy.com.chujia;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import java.io.File;


public class MainActivity extends AppCompatActivity {
    private Uri fileUri;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void image(View v){
        //创建一个拍照意图
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        //设置文件保存路径
        fileUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "a.jgp"));
        intent.putExtra(MediaStore.EXTRA_OUTPUT,fileUri);
        //启动拍照Activity
        startActivityForResult(intent,10);
    }

    public void video(View v){
        //创建一个摄像意图
        Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        //设置文件保存路径
        fileUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"a.3gp"));
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        //启动摄像Activity
        startActivityForResult(intent, 20);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == 10)
            Toast.makeText(this, "拍照成功", Toast.LENGTH_SHORT).show();
        if(requestCode == 20)
            Toast.makeText(this, "摄像成功", Toast.LENGTH_SHORT).show();
    }
}

 

posted @ 2016-08-04 10:43  被罚站的树  阅读(240)  评论(0编辑  收藏  举报