2024年3月11日发(作者:)

中国手机开发者联盟-----

Android调用手机拍照以及从相册选择照片

/*用来标识请求照相功能的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(ernalStorageDirectory() + "/DCIM/Camera");

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

private void doPickPhotoAction() {

Context context = ;

// Wrap our context to inflate list items using correct theme

final Context dialogContext = new ContextThemeWrapper(context,

_Light);

String cancel="返回";

String[] choices;

choices = new String[2];

choices[0] = getString(_photo); //拍照

choices[1] = getString(_photo); //从相册中选择

final ListAdapter adapter = new ArrayAdapter(dialogContext,

_list_item_1, choices);

final r builder = new r(

dialogContext);

le(ToContact);

gleChoiceItems(adapter, -1,

new kListener() {

public void onClick(DialogInterface dialog, int which)

{

s();

switch (which) {

第1页(共5页)

中国手机开发者联盟-----

case 0:{

String

status=ernalStorageState();

if((_MOUNTED)){//判断是否有SD卡

doTakePhoto();// 用户点击了从照相机

获取

}

else{

showToast("没有SD卡");

}

break;

}

case 1:

doPickPhotoFromGallery();// 从相册中去获

break;

}

}

});

ativeButton(cancel, new

kListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

s();

}

});

().show();

}

}

/**

* 拍照获取图片

*

*/

protected void doTakePhoto() {

try {

// Launch camera to take photo for selected contact

PHOTO_();// 创建照片的存储目录

mCurrentPhotoFile = new File(PHOTO_DIR, getPhotoFileName());//

给新照的照片文件命名

第2页(共5页)