1.调用系统照相机
- // 照相
- public void takePhotos(View view) {
- Intent cameraIntent = new Intent(
- android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
- startActivityForResult(cameraIntent, CAMERA_REQUEST);
- }
// 照相 public void takePhotos(View view) { Intent cameraIntent = new Intent( android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent, CAMERA_REQUEST); }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) { //此处进行处理
- // TODO Auto-generated method stub
- if(requestCode == CAMERA_REQUEST && resultCode == RESULT_OK && data.getExtras().get("data")!=null){
- if(rsd.getPhoto1URL() != null){ //删除上一张图片信息
- deleteImgage(rsd.getPhoto1URL());
- }
- Bitmap photo = (Bitmap)data.getExtras().get("data");
- File baseFile = FileHelper.getPicBaseFile();
- if(baseFile == null){
- showLongMessage("SD卡不可用,请检查SD卡情况");
- return;
- }
- String fileName = getFileName(); //图片名称
- rsd.setPhoto1URL(fileName);
- saveBitmap(photo, fileName, baseFile);
- imgPhoto.setImageBitmap(photo);
- }
- super.onActivityResult(requestCode, resultCode, data);
- }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { //此处进行处理 // TODO Auto-generated method stub if(requestCode == CAMERA_REQUEST && resultCode == RESULT_OK && data.getExtras().get("data")!=null){ if(rsd.getPhoto1URL() != null){ //删除上一张图片信息 deleteImgage(rsd.getPhoto1URL()); } Bitmap photo = (Bitmap)data.getExtras().get("data"); File baseFile = FileHelper.getPicBaseFile(); if(baseFile == null){ showLongMessage("SD卡不可用,请检查SD卡情况"); return; } String fileName = getFileName(); //图片名称 rsd.setPhoto1URL(fileName); saveBitmap(photo, fileName, baseFile); imgPhoto.setImageBitmap(photo); } super.onActivityResult(requestCode, resultCode, data); }
2.
- //获取文件名
- private String getFileName(){
- SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss",Locale.getDefault());
- Date cruDate = Calendar.getInstance().getTime();
- String strDate = sdf.format(cruDate);
- String userName = Config.getUser(Question_Record.this).getLoginName();
- String fileName = userName + strDate+".jpg";
- return fileName;
- }
//获取文件名 private String getFileName(){ SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss",Locale.getDefault()); Date cruDate = Calendar.getInstance().getTime(); String strDate = sdf.format(cruDate); String userName = Config.getUser(Question_Record.this).getLoginName(); String fileName = userName + strDate+".jpg"; return fileName; }3.
- //保存图片
- private void saveBitmap(Bitmap bitmap,String fileName,File baseFile){
- FileOutputStream bos = null;
- File imgFile = new File(baseFile,"/"+fileName);
- try {
- bos = new FileOutputStream(imgFile);
- bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally{
- try {
- bos.flush();
- bos.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
//保存图片 private void saveBitmap(Bitmap bitmap,String fileName,File baseFile){ FileOutputStream bos = null; File imgFile = new File(baseFile,"/"+fileName); try { bos = new FileOutputStream(imgFile); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { bos.flush(); bos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
4.文件帮助类
- private static String filePath = "inspect/image";
- /**
- * 图片基础路径
- * @return
- */
- public static File getPicBaseFile() {
- if (Environment.getExternalStorageState().equals(
- Environment.MEDIA_MOUNTED)) { //文件可用
- File f = new File(Environment.getExternalStorageDirectory(), filePath);
- if(!f.exists()) f.mkdirs();
- return f;
- }else{
- return null;
- }
- }
- }
public class FileHelper { private static String filePath = "inspect/image"; /** * 图片基础路径 * @return */ public static File getPicBaseFile() { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { //文件可用 File f = new File(Environment.getExternalStorageDirectory(), filePath); if(!f.exists()) f.mkdirs(); return f; }else{ return null; } } }
5.上述的代码已经很详细,如有看不懂的童鞋可和我联系