android 调用照相机拍照与图片选择图片选择

1.封装在OpenSystemPictureDialog 中,自己看看代码,布局就不贴了


public class OpenSystemPictureDialog extends PopupWindow implements

OnClickListener
{


/** 拍照 */
public static final int SELECT_PICTURE_FROM_CAMERA = 0x12;


/** 图库 */
public static final int SELECT_PICTURE_FROM_FILE = 0x13;


/** 上传之后返回 */
public static final int SELECT_PICTURE_PATHID = 0x14;


private Button btChatNow;
private Button btChatPic;
private IChatPic mChatPic;
private Context context;
/** 是否进入发送界面 */
private boolean isInSendUI = true;
/** 是否裁剪*/
private boolean doCrop = false;

/***
* 设置是否进入发送界面

* @param isInSendUI
*            默认 true:进入发送界面 false:不进入直接返回图片地址
*/
public void setInSendUI(boolean isInSendUI)
{
this.isInSendUI = isInSendUI;
}


public String getPhotoFileNameJPEG()
{
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd_HHmmss");
String dateFileName = df.format(new Date());
String fileName = dateFileName + "."
+ CompressFormat.JPEG.toString().toLowerCase();
return fileName;
}


public interface IChatPic
{
public void resultByPathID(String path, String pathID);
}


/**
* setInSendUI(boolean isInSendUI)设置是否进入发送页面,默认进入

* @param context
* @param chatPic
*/
public OpenSystemPictureDialog(Context context, IChatPic chatPic)
{
super(context);
init(context, chatPic);
}

public OpenSystemPictureDialog(Context context,boolean doCrop, IChatPic chatPic)
{
super(context);
init(context,doCrop, chatPic);
}


private void init(Context context, IChatPic chatPic)
{
init(context, false, chatPic);
}

private void init(Context context,boolean doCrop, IChatPic chatPic)
{
this.context = context;
this.mChatPic = chatPic;
View view = LayoutInflater.from(context).inflate(
R.layout.chat_dialog_choose_picture, null);
btChatNow = (Button) view.findViewById(R.id.btChatNow);
btChatPic = (Button) view.findViewById(R.id.btChatPic);
btChatNow.setOnClickListener(this);
btChatPic.setOnClickListener(this);
view.setFocusableInTouchMode(true);
this.setFocusable(true);
this.setContentView(view);
ColorDrawable colorDrawable = new ColorDrawable(R.color.transparent);
colorDrawable.setAlpha(0);
this.setBackgroundDrawable(colorDrawable);
this.setWidth(android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
this.setHeight(android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
this.doCrop = doCrop;
if (doCrop)
{
this.isInSendUI = false;
}
}


/**
* 显示

* @param view
*/
public void show(View view)
{
showAtLocation(view, Gravity.CENTER, 0, 0);
}


private Uri imageCaptureUri;


@Override
public void onClick(View v)
{
if (v == btChatNow)
{
FileUtil.makeDirectory(Constants.PIC_PATH);
// 拍照
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imageCaptureUri = Uri.fromFile(new File(Constants.PIC_PATH,
"couple_" + getPhotoFileNameJPEG()));
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageCaptureUri);
((Activity) context).startActivityForResult(intent,
SELECT_PICTURE_FROM_CAMERA);
} else if (v == btChatPic)
{
// 图库
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
Intent wrapi = Intent.createChooser(intent, "选择图片");
((Activity) context).startActivityForResult(wrapi,
SELECT_PICTURE_FROM_FILE);
}
dismiss();
}


/**
* 在Activity onActivityResult() 使用

* @param requestCode
* @param resultCode
* @param data
*/
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == Activity.RESULT_OK)
{
switch (requestCode)
{
case SELECT_PICTURE_PATHID:
{
String pathID = data
.getStringExtra(PhotoSendActivity.PATHID);
String path = data.getStringExtra(PhotoSendActivity.PATH);
mChatPic.resultByPathID(path, pathID);
break;
}
case SELECT_PICTURE_FROM_CAMERA:
{
if ("file".equals(imageCaptureUri.getScheme())) 
{// file://開頭的URI
if (doCrop)
{
Intent intent = new Intent(context, ImageFilterCropActivity.class);
intent.putExtra(PhotoSendActivity.PATH,
imageCaptureUri.getPath());
((Activity) context).startActivityForResult(intent,
SELECT_PICTURE_PATHID);
}else if (isInSendUI)
{
Log.e("", "onPostExecute==" + imageCaptureUri.getPath());
Intent intent = new Intent(context, PhotoSendActivity.class);
intent.putExtra(PhotoSendActivity.PATH, imageCaptureUri.getPath());
((Activity) context).startActivityForResult(intent,
SELECT_PICTURE_PATHID);
}
else {
// 返回选择图片地址
mChatPic.resultByPathID(imageCaptureUri.getPath(), null);
}

}
break;
}
case SELECT_PICTURE_FROM_FILE:
{
Uri selectedImageUri = data.getData();
new AsySearchPhoto().execute(selectedImageUri);
break;
}
}
}
}


private class AsySearchPhoto extends AsyncTask
{
@Override
protected String doInBackground(Uri... params)
{
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = MyApplication.getContext().getContentResolver()
.query(params[0], projection, null, null, null);
String path = null;
if (cursor != null)
{
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
path = cursor.getString(column_index);
try
{
// 4.0以上的版本会自动关闭 (4.0--14;; 4.0.3--15)
if (Integer.parseInt(Build.VERSION.SDK) < 14)
{
cursor.close();
}
} catch (Exception e)
{
}
}

return path;
}


@Override
protected void onPostExecute(String result)
{
if (!StringUtils.isNullOrEmpty(result))
{
if (doCrop)
{
Intent intent = new Intent(context, ImageFilterCropActivity.class);
intent.putExtra(PhotoSendActivity.PATH,
result);
((Activity) context).startActivityForResult(intent,
SELECT_PICTURE_PATHID);
}else if (isInSendUI)
{
Log.e("", "onPostExecute==" + result);
Intent intent = new Intent(context, PhotoSendActivity.class);
intent.putExtra(PhotoSendActivity.PATH, result);
((Activity) context).startActivityForResult(intent,
SELECT_PICTURE_PATHID);

}
else 
{
// 返回选择图片地址
mChatPic.resultByPathID(result, null);
}
}
}
}


public void destory()
{
btChatNow = null;
btChatPic = null;
mChatPic = null;
context = null;
}
}


你可能感兴趣的:(android经验总结)