protected void getImageFromCamera() { String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) { Intent intent = new Intent(); intent.setAction("android.media.action.IMAGE_CAPTURE"); startActivityForResult(intent, 2); } else { Toast.makeText(getApplicationContext(), "请确认已经插入SD卡", Toast.LENGTH_LONG).show(); } }
当图片的信息返回后,我们要在onActivityResult方法中获取返回的图片信息。
1 2 3 4 5 6 7 8 9 10
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 1) { Uri uri = data.getData(); //to do find the path of pic
} else if (requestCode == 2 ) { Uri uri = data.getData(); //to do find the path of pic } }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 1) { Uri uri = data.getData(); //to do find the path of pic by uri
} else if (requestCode == 2 ) { Uri uri = data.getData(); if(uri == null){ //use bundle to get data Bundle bundle = data.getExtras(); if (bundle != null) { Bitmap photo = (Bitmap) bundle.get("data"); //get bitmap //spath :生成图片取个名字和路径包含类型 saveImage(Bitmap photo, String spath); } else { Toast.makeText(getApplicationContext(), "err****", Toast.LENGTH_LONG).show(); return; } }else{ //to do find the path of pic by uri } } }