android camera Intent调用

http://www.cnblogs.com/pandans/archive/2011/05/29/2062455.html

/*用来标识请求照相功能的activity*/
private static final int CAMERA_WITH_DATA = 3023;

/*用来标识请求gallery的activity*/
private static final int PHOTO_PICKED_WITH_DATA = 3021;

/*拍照的照片存储位置*/
private static final File PHOTO_DIR = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera");

注意此处 写的是sd卡路径需加 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 权限

如果此处写成程序路径 拍照以后 不会调用onActivityResult 原因外边程序无法访问本程序

private File mCurrentPhotoFile;//照相机拍照得到的图片

private void doPickPhotoAction() {
Context context = EditContact.this;

// Wrap our context to inflate list items using correct theme
final Context dialogContext = new ContextThemeWrapper(context,
android.R.style.Theme_Light);
String cancel="返回";
String[] choices;
choices = new String[2];
choices[0] = getString(R.string.take_photo);//拍照
choices[1] = getString(R.string.pick_photo);//从相册中选择
final ListAdapter adapter = new ArrayAdapter<String>(dialogContext,
android.R.layout.simple_list_item_1, choices);

final AlertDialog.Builder builder = new AlertDialog.Builder(
dialogContext);
builder.setTitle(R.string.attachToContact);
builder.setSingleChoiceItems(adapter, -1,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
switch (which) {
case 0:{
String status=Environment.getExternalStorageState();
if(status.equals(Environment.MEDIA_MOUNTED)){//判断是否有SD卡
doTakePhoto();// 用户点击了从照相机获取
}
else{
showToast("没有SD卡");
}
break;

}
case 1:
doPickPhotoFromGallery();// 从相册中去获取
break;
}
}
});
builder.setNegativeButton(cancel, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}

});
builder.create().show();
}
}

/**
* 拍照获取图片
*
*/
protected void doTakePhoto() {
try {
// Launch camera to take photo for selected contact
PHOTO_DIR.mkdirs();// 创建照片的存储目录
mCurrentPhotoFile = new File(PHOTO_DIR, getPhotoFileName());// 给新照的照片文件命名
final Intent intent = getTakePickIntent(mCurrentPhotoFile);
startActivityForResult(intent, CAMERA_WITH_DATA);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, R.string.photoPickerNotFoundText,
Toast.LENGTH_LONG).show();
}
}

public static Intent getTakePickIntent(File f) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, null);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
return intent;
}

/**
* 用当前时间给取得的图片命名
*
*/
private String getPhotoFileName() {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat dateFormat = new SimpleDateFormat(
"'IMG'_yyyy-MM-dd HH:mm:ss");
return dateFormat.format(date) + ".jpg";
}

// 请求Gallery程序
protected void doPickPhotoFromGallery() {
try {
// Launch picker to choose photo for selected contact
final Intent intent = getPhotoPickIntent();
startActivityForResult(intent, PHOTO_PICKED_WITH_DATA);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, R.string.photoPickerNotFoundText1,
Toast.LENGTH_LONG).show();
}
}

// 封装请求Gallery的intent
public static Intent getPhotoPickIntent() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 80);
intent.putExtra("outputY", 80);
intent.putExtra("return-data", true);
return intent;
}

// 因为调用了Camera和Gally所以要判断他们各自的返回情况,他们启动时是这样的startActivityForResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK)
return;
switch (requestCode) {
case PHOTO_PICKED_WITH_DATA: {// 调用Gallery返回的
final Bitmap photo = data.getParcelableExtra("data");
// 下面就是显示照片了
System.out.println(photo);
//缓存用户选择的图片
img = getBitmapByte(photo);
mEditor.setPhotoBitmap(photo);
System.out.println("set new photo");
break;
}
case CAMERA_WITH_DATA: {// 照相机程序返回的,再次调用图片剪辑程序去修剪图片
doCropPhoto(mCurrentPhotoFile);
break;
}
}
}

protected void doCropPhoto(File f) {
try {
// 启动gallery去剪辑这个照片
final Intent intent = getCropImageIntent(Uri.fromFile(f));
startActivityForResult(intent, PHOTO_PICKED_WITH_DATA);
} catch (Exception e) {
Toast.makeText(this, R.string.photoPickerNotFoundText,
Toast.LENGTH_LONG).show();
}
}

/**
* Constructs an intent for image cropping. 调用图片剪辑程序
*/
public static Intent getCropImageIntent(Uri photoUri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(photoUri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 80);
intent.putExtra("outputY", 80);
intent.putExtra("return-data", true);
return intent;
}

更多相关文章

  1. Qt5.1.1 for android 环境配置
  2. android中实现从一个图片中截取一部分,在翻转,成一个圆形显示
  3. DelphiXE Android的所有权限按照分类总结说明
  4. android Intent的一些用法
  5. Android(安卓)Intent
  6. Android(安卓)如何退出程序
  7. android perimission 和 user-perimission
  8. android 调用打电话URI
  9. Android(安卓)学习笔记-2011年7月

随机推荐

  1. Android 进程间通信——AIDL
  2. Android Drawable
  3. android view滑动助手类OverScroller
  4. Android官方DataBinding(十):双向绑定之基于
  5. Android引路蜂地图开发示例:概述
  6. Chris:怎样成为一名Android应用开发
  7. android sqlite用管理工具查看
  8. Android中SharedPrerence的apply和commit
  9. Android声音管理AudioManager使用
  10. Android第三方登陆之新浪微博Weibo篇(原生