【发布时间】:2013-11-08 01:28:09
【问题描述】:
我有一个关于如何使用相机意图(或相机 API)拍摄图像,然后将图像放入 imageView 以在我的应用程序中显示的问题。这是我目前所拥有的。
我设置了一个按钮
Button btnPicture = (Button) findViewById(R.id.btn_picture);
btnPicture.setOnClickListener(this);
我设置了一个相机方法
private void Camera() {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PICTURE_CODE);
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, REQUEST_CODE);
}
这就是我迷路的地方。我正在尝试处理我拍摄的图像。
private void processImage(Intent intent) {
setContentView(R.layout.imagelayout);
ImageView imageView = (ImageView)findViewById(R.id.image_view);
cameraBitmap = (Bitmap)intent.getExtras().get("data");
imageView.setImageBitmap(cameraBitmap);
}
我的目的是显示您在 image_view 中拍摄的图像。我没有收到错误,没有任何反应。当我拍照时,系统会要求我再拍一张照片,或者在我使用设备后退按钮后,应用程序会关闭。看来我完全退出了我的申请,返回是一个大问题。有什么建议?我错过了什么?
是的,这是我的 onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(TAKE_PICTURE_CODE == requestCode) {
Bundle extras = data.getExtras();
if (extras.containsKey("data")) {
Bitmap bmp = (Bitmap) extras.get("data");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] image = baos.toByteArray();
if (image != null) {
Log.d(TAG, "image != null");
}
} else {
Toast.makeText(getBaseContext(), "Fail to capture image", Toast.LENGTH_LONG).show();
}
}
}
我正在尝试将图像放入 getExtras,然后将其存储到 ByteArray。这是我想做的另一件事。不知道这一切是如何结合在一起的。
【问题讨论】:
-
发布
onActivityResult()..的代码。 -
这是一个很好的项目,可以处理大部分复杂性:github.com/CameraKit/camerakit-android。不需要
onActivityResult(..),简洁的onRequestPermissionsResult(..)实现。在这个问题的 [100 多行] 答案的海洋中,非常感谢:stackoverflow.com/a/43707269/2162226
标签: android android-camera android-imageview