Search code examples
javaandroidandroid-cameraandroid-imageview

How to add an image captured on camera to imageviewer in Android using java? I have tried the following code


I have added Camera permissions as well which is working perfectly but the image view is not holding the image that is captured. The manifest file is also proper still. The app isn't crashing even it is not showing any errors as well. And I even want to add the image to the database.

    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);
                    }
                }

            });


        }
    }

Solution

  •     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);
            }
        }