Android拍照显示

源码是从公司项目分离出来的...

转帖请贴明出处:http://blog.csdn.net/yezisuifengbiao/article/details/10431971

public static final String SDCARD_ROOT_PATH = android.os.Environment
			.getExternalStorageDirectory().getAbsolutePath();// 路径
	public static final String SAVE_PATH_IN_SDCARD = "/testcamera/"
	public static final String IMAGE_CAPTURE_NAME = "cameraTmp.png"; // 照片名称
	public final static int REQUEST_CODE_TAKE_PICTURE = 12;// 设置拍照操作的标志

其实,调用系统相机是最方便,最快捷,最简单的,简单到只需要一句话就可以解决:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

首先,要先检查SD卡是否插入:

public static boolean isHasSdcard() {
		String status = Environment.getExternalStorageState();
		if (status.equals(Environment.MEDIA_MOUNTED)) {
			return true;
		} else {
			return false;
		}
	}

给一个判断,当存储卡可用的状态下,把照片存在SD卡

	if (isHasSdcard()) {
				File vFile = new File(SDCARD_ROOT_PATH + SAVE_PATH_IN_SDCARD, IMAGE_CAPTURE_NAME);
					if (!vFile.exists()) {
						File vDirPath = vFile.getParentFile();
						vDirPath.mkdirs();
					}

					Uri uri = Uri.fromFile(vFile);
					intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);

				}
	startActivityForResult(intent, REQUEST_CODE_TAKE_PICTURE);

当我们拍完照片之后,回调回来的时候,需要重写 onActivityResult

如果SD卡可用,则从存储路径下,取得图片。相反,则直接返回缩略图

当然,现在手机照出来的照片都很大,不可能显示出来的时候,满满整张大图。需要用一个算法,来等比例缩放

Bitmap bitmap;
int maxH = 200;
private void getImage(String imagePath) {
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		
		bitmap = BitmapFactory.decodeFile(imagePath, options); 
		
		int be = (int) (options.outHeight / (float) maxH);
		int ys = options.outHeight % maxH;
		float fe = ys / (float) maxH;
		if (fe >= 0.5)
			be = be + 1;
		if (be <= 0)
			be = 1;
		options.inSampleSize = be;

		options.inJustDecodeBounds = false;
		bitmap = BitmapFactory.decodeFile(imagePath, options);
		mimg_show.setImageBitmap(bitmap);
		mimg_show.setVisibility(View.VISIBLE);
}

点我-下载源码

你可能感兴趣的:(Android例子)