Androidがカメラから画像を取得する2つの方法

2203 ワード

方法1:
この方法ではCameraが直接写真を生成してアプリケーションに返信しますが、圧縮画像が返され、表示がはっきりしません.
	try {
		 Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		 startActivityForResult(cameraIntent, CAMERA_WITH_DATA);
	 } catch (ActivityNotFoundException e) {
		 e.printStackTrace();
	 }
Bundle bundle = data.getExtras();
bmp_selectedPhoto = (Bitmap) bundle.get("data");
if (bmp_selectedPhoto != null)
    bmp_selectedPhoto.recycle();
bmp_selectedPhoto = (Bitmap) data.getExtras().get("data");
// int scale = ImageThumbnail.reckonThumbnail(bitMap.getWidth(),
// bitMap.getHeight(), 500, 600);
// bitMap = ImageThumbnail.PicZoom(bitMap,
// (int) (bitMap.getWidth() / scale),
// (int) (bitMap.getHeight() / scale));
// bitMap = ThumbnailUtils.extractThumbnail(bitMap, 200, 200);
if(bmp_selectedPhoto != null){
    home_view.setBackground(new BitmapDrawable(getResources(),
    bmp_selectedPhoto));
}

方法2:
この方法では撮影されますが、Sdカードに一時ファイルが生成されます.
Intent cameraIntent = new Intent(
				android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

		File appDir = new File(Environment.getExternalStorageDirectory()
				+ "/KengDieA");
		
		if (!appDir.exists()) {
			appDir.mkdir();
		}

		mUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory()
				+ "/KengDieA/", "kengDiePic"
				+ String.valueOf(System.currentTimeMillis()) + ".jpg"));
		cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mUri);

		try {
			cameraIntent.putExtra("return-data", true);
			startActivityForResult(cameraIntent, CAMERA_WITH_DATA);
		} catch (Exception e) {
			e.printStackTrace();
		}
ContentResolver cr = this.getContentResolver();
			try {
				if (bmp_selectedPhoto != null)//        ,     ,      
					bmp_selectedPhoto.recycle();
				bmp_selectedPhoto = BitmapFactory.decodeStream(cr
						.openInputStream(mUri));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			home_view.setBackground(new BitmapDrawable(getResources(),
					bmp_selectedPhoto));