android调用系统照相机并保持图片到本地 .

1.调用系统照相机

 

  1. // 照相   
  2. public void takePhotos(View view) {  
  3.     Intent cameraIntent = new Intent(  
  4.             android.provider.MediaStore.ACTION_IMAGE_CAPTURE);  
  5.     startActivityForResult(cameraIntent, CAMERA_REQUEST);  
  6. }  
	// 照相
	public void takePhotos(View view) {
		Intent cameraIntent = new Intent(
				android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
		startActivityForResult(cameraIntent, CAMERA_REQUEST);
	}
  1. @Override  
  2.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {   //此处进行处理   
  3.         // TODO Auto-generated method stub   
  4.         if(requestCode == CAMERA_REQUEST && resultCode == RESULT_OK && data.getExtras().get("data")!=null){  
  5.             if(rsd.getPhoto1URL() != null){  //删除上一张图片信息   
  6.                 deleteImgage(rsd.getPhoto1URL());  
  7.             }  
  8.             Bitmap photo = (Bitmap)data.getExtras().get("data");  
  9.             File baseFile = FileHelper.getPicBaseFile();  
  10.             if(baseFile == null){  
  11.                 showLongMessage("SD卡不可用,请检查SD卡情况");  
  12.                 return;  
  13.             }  
  14.             String fileName = getFileName();   //图片名称   
  15.             rsd.setPhoto1URL(fileName);  
  16.             saveBitmap(photo, fileName, baseFile);  
  17.             imgPhoto.setImageBitmap(photo);  
  18.         }  
  19.           
  20.         super.onActivityResult(requestCode, resultCode, data);  
  21.     }  
@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.
  1. //获取文件名   
  2. private String getFileName(){  
  3.     SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss",Locale.getDefault());  
  4.     Date cruDate = Calendar.getInstance().getTime();  
  5.     String strDate = sdf.format(cruDate);  
  6.     String userName = Config.getUser(Question_Record.this).getLoginName();  
  7.     String fileName = userName + strDate+".jpg";  
  8.     return fileName;  
  9. }  
	//获取文件名
	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.
  1. //保存图片   
  2.     private void saveBitmap(Bitmap bitmap,String fileName,File baseFile){  
  3.         FileOutputStream bos = null;  
  4.         File imgFile = new File(baseFile,"/"+fileName);  
  5.         try {  
  6.             bos = new FileOutputStream(imgFile);  
  7.             bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);  
  8.         } catch (FileNotFoundException e) {  
  9.             // TODO Auto-generated catch block   
  10.             e.printStackTrace();  
  11.         }finally{  
  12.             try {  
  13.                 bos.flush();  
  14.                 bos.close();  
  15.             } catch (IOException e) {  
  16.                 // TODO Auto-generated catch block   
  17.                 e.printStackTrace();  
  18.             }  
  19.         }  
  20.     }  
//保存图片
	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.文件帮助类

 

public class FileHelper {  
  1.     private static String filePath = "inspect/image";  
  2.       
  3.     /** 
  4.      * 图片基础路径 
  5.      * @return 
  6.      */  
  7.     public static File getPicBaseFile() {  
  8.         if (Environment.getExternalStorageState().equals(  
  9.                 Environment.MEDIA_MOUNTED)) {  //文件可用   
  10.             File f = new File(Environment.getExternalStorageDirectory(), filePath);  
  11.             if(!f.exists()) f.mkdirs();  
  12.             return f;  
  13.         }else{  
  14.             return null;  
  15.         }  
  16.     }  
  17.       
  18. }  
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.上述的代码已经很详细,如有看不懂的童鞋可和我联系
posted @ 2013-01-21 16:12  疯子FK  阅读(551)  评论(0编辑  收藏  举报