在 Android 中使用相机活动 [英] Using the camera activity in Android
问题描述
如果您想使用使用原生 Android 相机的内置相机活动,只需执行以下操作.
If you want to use the built-in camera activity which uses the native Android camera, simply do the following.
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
this.startActivityForResult(camera, PICTURE_RESULT);
您想从您展示的漂亮相机中取回图像——但是如何?
You want to get the images back from the nifty camera you displayed -- but how?
推荐答案
如果您想让图像恢复原状,请在 EXTRA_OUTPUT extra 中将 uri 传递给 Intent.如果您对较小的位图感到满意(您应该如此),只需像往常一样调用 Intent.
If you want to get the image back in its full glory, pass in a uri to the Intent within the EXTRA_OUTPUT extra. If you're fine with a smallish bitmap (and you should be), just call the intent as normal.
现在您有两个选择,处理在 EXTRA_OUTPUT extra 中返回的图像的 uri,或者在您的 onActivityResult 方法中执行以下操作:
Now you have two options, deal with the uri of the image that is returned in the EXTRA_OUTPUT extra, or do the following in your onActivityResult method:
if (requestCode == PICTURE_RESULT) //
if (resultCode == Activity.RESULT_OK) {
// Display image received on the view
Bundle b = data.getExtras(); // Kept as a Bundle to check for other things in my actual code
Bitmap pic = (Bitmap) b.get("data");
if (pic != null) { // Display your image in an ImageView in your layout (if you want to test it)
pictureHolder = (ImageView) this.findViewById(R.id.IMAGE);
pictureHolder.setImageBitmap(pic);
pictureHolder.invalidate();
}
}
else if (resultCode == Activity.RESULT_CANCELED) {...}
}
就这样!
这篇关于在 Android 中使用相机活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!