大部分业务可以通过调用系统的相机程序来拍照。
界面如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="前往拍照" android:onClick="goToPicture" /> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
java:
package com.example.usingSystemCamera; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.view.View; import android.widget.ImageView; import java.io.File; public class MyActivity extends Activity { private File file;//相片文件 private ImageView imageView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); imageView = (ImageView) findViewById(R.id.imageView); } public void goToPicture(View view) { Intent intent = new Intent(); intent.setAction("android.media.action.IMAGE_CAPTURE"); intent.addCategory("android.intent.category.DEFAULT"); file = new File(Environment.getExternalStorageDirectory(), "myJpg.jpg"); Uri uri = Uri.fromFile(file); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); startActivityForResult(intent, 0); } /** * 当保存了文件后,设置到界面上 */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { Bitmap bitmap = BitmapFactory.decodeFile(file.getPath(), null); imageView.setImageBitmap(bitmap); } }