public class MainActivity extends Activity implements OnClickListener {
private Button btn_photo;
private Button btn_camare;
private ImageView img;
private static final int CAMARE = 999;
private static final int PHOTO = 888;
private static final int CAMARE_SD = 666;
private File imgFile;
private Uri imgUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initview();
}
private void initview() {
// TODO Auto-generated method stub
btn_photo = (Button)findViewById(R.id.btn_photo);
btn_camare = (Button)findViewById(R.id.btn_camare);
img = (ImageView)findViewById(R.id.img);
btn_camare.setOnClickListener(this);
btn_photo.setOnClickListener(this);
}
重写onActivityResult方法 写好三个请求码
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case CAMARE:
if (requestCode == RESULT_CANCELED) {
Toast.makeText(this,"取消了拍照",Toast.LENGTH_SHORT).show();
return;
}
Bitmap camarebit = data.getParcelableExtra("data");
img.setImageBitmap(camarebit);
break;
case PHOTO:
if (resultCode==RESULT_OK) {
imgUri = data.getData();
img.setImageURI(imgUri);
}
break;
case CAMARE_SD:
// if (hasSDcard()) {
// img.setImageURI(imgUri);
//
// }
if (resultCode==RESULT_OK) {
String path = getPathFromUri(imgUri);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize=5;
Bitmap bit = BitmapFactory.decodeFile(path, options);
img.setImageBitmap(bit);
}
break;
}
}
点击事件
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_camare:
// Intent in = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// if (hasSDcard()) {
// File dir = getExternalCacheDir();
// imgFile = new File(dir,System.currentTimeMillis()+"avatar.png");
// imgUri = Uri.fromFile(imgFile);
//
// in.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);
// startActivityForResult(in, CAMARE_SD);
// }
intent 跳转到系统相机,给一个之前写好的请求码
Intent camarein = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imgUri = creatUri();
getPathFromUri(imgUri);
camarein.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);
startActivityForResult(camarein,CAMARE_SD);
break;
case R.id.btn_photo:
Intent in = new Intent(Intent.ACTION_GET_CONTENT);
in.setType("img/*");
startActivityForResult(in,PHOTO);
break;
}
}
判断sd卡是否存在
private boolean hasSDcard(){
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
return true;
}else {
return false;
}
}
private Uri creatUri() {
// TODO Auto-generated method stub
String imgName = System.currentTimeMillis()+"avatar.jepg";
ContentValues value = new ContentValues();
value.put(MediaStore.Images.Media.TITLE,imgName);
value.put(MediaStore.Images.Media.DISPLAY_NAME,imgName);
value.put(MediaStore.Images.Media.MIME_TYPE,imgName );
Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,value);
return uri;
}
获取 Uri 的方法
private String getPathFromUri(Uri uri) {
// TODO Auto-generated method stub
if (uri!=null) {
String path = null;
先初始化path
获得uri的头
判断是File还是content
String scheme = uri.getScheme();
if (ContentResolver.SCHEME_FILE.equals(scheme)) {
path = uri.getPath();
}else if(ContentResolver.SCHEME_CONTENT.equals(scheme)){
Cursor cursor = getContentResolver().query(uri, new String[] {ImageColumns.DATA}, null,null,null);
if (cursor.moveToFirst()) {
int index = cursor.getColumnIndex(ImageColumns.DATA);
path = cursor.getString(index);
}
cursor.close();
}
return path;
}
return null;
}
}