android - 如何在android中的imageview中显示相机拍摄的照片

标签 android

任何人都可以告诉如何在android中使用前置摄像头拍摄的imageview照片中显示图片任何人都可以提供代码

谢谢

最佳答案

这是代码,请尝试一下

这是我的 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:id="@+id/linear">
    <Button android:id="@+id/btn" android:layout_width="200dp"
        android:layout_height="wrap_content" android:text="Click" />
    <ImageView android:id="@+id/img_preview"
        android:layout_width="fill_parent" android:layout_height="fill_parent" />
</LinearLayout>

这是 Activity 代码

Button btn;
int CAMERA_PIC_REQUEST = 0;
ImageView imgView;

imgView = (ImageView) findViewById(R.id.img_preview);

    btn = (Button) findViewById(R.id.btn);
    btn.setCompoundDrawables(null, getResources().getDrawable(R.drawable.icon), null, null);
    btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent camera_intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(camera_intent, CAMERA_PIC_REQUEST);
                    }
      });

这是 onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode){
    case CAMERA_PIC_REQUEST:
        if(resultCode==RESULT_OK){
           Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
           imgView.setImageBitmap(thumbnail);
            }
    }
}

关于android - 如何在android中的imageview中显示相机拍摄的照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6994559/

相关文章:

java - 就位比例矩阵

java - Android:从变量设置按钮的背景

android - 将结束与元素开始相对布局对齐

android - 在 API 11 之前可见时修改选项菜单

android - 如何从另一个自定义 XML 布局引用自定义 XML 布局?

java - 使用 Fresco 无需服务器请求即可替换图像

android - 从 AppWidgetProvider 启动 WorkManager 任务会导致无休止的 onUpdate 调用

android - Activity 跳转开始时的动画

android - 在 android 中从 Shoutcast Url 播放

android - 从图库中删除图像 - android