实现调用Android手机的拍照功能

很简单,直接上示例吧

1 xml

 <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <Button
android:id="@+id/btnTakePic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拍照" /> <ImageView
android:id="@+id/imgview"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignLeft="@+id/btnTakePic"
android:layout_below="@+id/btnTakePic"
android:layout_marginTop="38dp" /> </RelativeLayout>

2 java代码

 package com.example.takepictrures;

 import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.example.util.saveToSdCard;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio.Media;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView; public class MainActivity extends Activity implements OnClickListener { public Button button;
private static final int PHOTO_GRAPH = 1;// ����
public ImageView imageView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) this.findViewById(R.id.imgview);
button = (Button) this.findViewById(R.id.btnTakePic);
button.setOnClickListener(this); } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnTakePic:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, PHOTO_GRAPH);
break; default:
break;
}
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == NONE)
return; if (requestCode == PHOTO_GRAPH) { File picture = new File(Environment.getExternalStorageDirectory()
+ "/temp.jpg");
// startPhotoZoom(Uri.fromFile(picture));
} if (data == null)
return; // if (requestCode == PHOTO_ZOOM) {
// startPhotoZoom(data.getData());
// } String fileName = "";
if (requestCode == 1) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);// (0-100)ѹ���ļ�

///这儿保存到sd卡就Ok了
try {
fileName = saveToSdCard.getFileName();
saveToSdCard.saveMyBitmap(photo, fileName);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
imageView.setImageBitmap(photo);
}
}
super.onActivityResult(requestCode, resultCode, data);
} // public void startPhotoZoom(Uri uri) {
// Intent intent = new Intent("com.android.camera.action.CROP");
// intent.setDataAndType(uri, IMAGE_UNSPECIFIED);
// intent.putExtra("crop", "true");
// // aspectX aspectY
// intent.putExtra("aspectX", 1);
// intent.putExtra("aspectY", 1);
// // outputX outputY
// intent.putExtra("outputX", 300);
// intent.putExtra("outputY", 500);
// intent.putExtra("return-data", true);
// startActivityForResult(intent, PHOTO_RESOULT);
// } }
上一篇:poj 3190 贪心+优先队列优化


下一篇:Apache Spark源码走读之21 -- WEB UI和Metrics初始化及数据更新过程分析