标签:
通过隐式意图来打开照相机
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <Button android:onClick="click" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="拍照" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/iv" /> </LinearLayout>
package com.itheima.camera; import java.io.File; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.view.View; import android.widget.ImageView; public class MainActivity extends Activity { private ImageView iv; private File file ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.iv); } //打开的是前置摄像头 public void click(View view) { Intent intent = new Intent(); // 指定拍照的意图。 // MediaStore.ACTION_VIDEO_CAPTURE是录像的意图 intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE); file = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg"); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); // 指定保存文件的路径 startActivityForResult(intent, 100); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode==100){ iv.setImageURI(Uri.fromFile(file)); } super.onActivityResult(requestCode, resultCode, data); } }
标签:
原文地址:http://my.oschina.net/u/2356176/blog/420338