如何使用java将相机拍摄的图像添加到Android中的imageviewer?我试过下面的代码

Posted

技术标签:

【中文标题】如何使用java将相机拍摄的图像添加到Android中的imageviewer?我试过下面的代码【英文标题】:How to add an image captured on camera to imageviewer in Android using java? I have tried the following code 【发布时间】:2020-03-08 00:23:17 【问题描述】:

我还添加了相机权限,该权限运行良好,但图像视图未保存捕获的图像。清单文件仍然是正确的。该应用程序没有崩溃,即使它也没有显示任何错误。我什至想将图像添加到数据库中。

    public class RiderProfile extends AppCompatActivity 

        ImageView imgDp,imgDlFront,imgDlback;
        TextView txtDp,txtDl;
        Button btnSave;
        public  static final int CAMERA_REQUEST = 1888;
        private static final String TAG = "1111";
        private static final int MY_CAMERA_PERMISSION_CODE = 100;

        @Override
        protected void onCreate(Bundle savedInstanceState) 
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_rider_profile);

            imgDp = (ImageView)findViewById(R.id.imgDp);
            imgDlFront = (ImageView)findViewById(R.id.imgDlFront);
            imgDlback = (ImageView)findViewById(R.id.imgDlback);
            txtDp = (TextView) findViewById(R.id.txtDp);
            txtDl = (TextView)findViewById(R.id.txtDl);
            btnSave = (Button) findViewById(R.id.btnSave);


            imgDp.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View v) 
                    Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(i, CAMERA_REQUEST);
                
                protected void onActivityResult(int requestCode, int resultCode, Intent data) 
                    if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) 
                        Bitmap photo1 = (Bitmap) data.getExtras().get("data");
                        Log.d(TAG, "onActivityResult: click ");
                        imgDp.setImageBitmap(photo1);
                    
                

            );


            imgDlFront.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View v) 
                    Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(i, CAMERA_REQUEST);
                
                protected void onActivityResult(int requestCode, int resultCode, Intent data) 
                    if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) 
                        Bitmap photo2 = (Bitmap) data.getExtras().get("data");
                        Log.d(TAG, "onActivityResult: click ");
                        imgDlFront.setImageBitmap(photo2);
                    
                

            );



            imgDlback.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View v) 
                    Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(i, CAMERA_REQUEST);
                
                protected void onActivityResult(int requestCode, int resultCode, Intent data) 
                    if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) 
                        Bitmap photo3 = (Bitmap) data.getExtras().get("data");
                        Log.d(TAG, "onActivityResult: click ");
                        imgDlback.setImageBitmap(photo3);
                    
                

            );


        
    

【问题讨论】:

【参考方案1】:
    1.  @Override
        public void onCameraOpen() 
          Intent pictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (pictureIntent.resolveActivity(getPackageManager()) != null) 
                try 
                    imageFile = CameraUtils.createImageFile(this);
                 catch (IOException e) 
                    e.printStackTrace();
                    return;
                
                imageUri = FileProvider.getUriForFile(this, getPackageName() + ".provider", imageFile);
                pictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                startActivityForResult(pictureIntent, IntentRequestCode.RESULT_CODE_IMAGE_CAPTURE);
            
        

2.  @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) 
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) 
            case RESULT_CODE_IMAGE_CAPTURE:
                if (resultCode == RESULT_OK) 
                    onCaptureImage(imageFile, imageUri);
                 else 
                    Toast.makeText(this, "Camera canceled", Toast.LENGTH_SHORT).show();
                
                break;
        
    

3. void onCaptureImage(File imageFile, Uri imageUri) 
        Uri uri = Uri.fromFile(imageFile);
        String selectedImagePath = CameraUtils.getPath(application, uri);
        File file1 = new File(selectedImagePath);
        if (file1.length() != 0) 
           FileAttachments b_data = new FileAttachments();
            b_data.setFileName(file1.getName());
            CameraUtils.writeScaledDownImage(file1, getApplication());
            b_data.setFile(file1);
        
    

【讨论】:

以上是关于如何使用java将相机拍摄的图像添加到Android中的imageviewer?我试过下面的代码的主要内容,如果未能解决你的问题,请参考以下文章

如何将图像从相机/UIImagePickerController 动态添加到 iPhone 中的 iCarousel

如何从相机捕获多张图像并使用位图将其编码为字符串?

如何使用opencv将鱼眼相机拍摄的图像转换为平面(矩形)图像?

如何在android中保存从相机拍摄的图像

Swift如何修改从移动相机拍摄的图像中的exif信息

如何从使用 pdfkit 相机拍摄的图像创建 pdf