storing multiple photos with android studio and sqlite

Viewed 15

storing multiple photos with android studio and sqlite

I am creating a CRUD with photo storage, I am trying to add two options for add the image, by the direct camera option and by gallery. but I have an error when selecting the camera option because it takes the photo (but does not direct me to the crop image library) and does not store the image "this is from the camera option". from the gallery option if it allows me to select the image and add it...

I would also like to know if you can help me to correct my code, to allow me from onActivityResult to select each image to be stored in each CircleImageView, since at the moment it only allows me to store one image. I appreciate your help

attached registration class

public class Registro extends AppCompatActivity implements View.OnClickListener {

    //variables para calendario
    private EditText efecha;
    private int dia,mes,ano;

    //View
    private CircleImageView imgPlaca, imgContenedor, imgSello;
    private EditText fechaIngresoEt, placaEt, nombreAuditEt;
    private Button Guardar;

    //actionbar
    private ActionBar actionBar;

    //Permiso de la clase Constants
    private static final int CAMERA_REQUEST_CODE = 100;
    private static final int STORAGE_REQUEST_CODE = 101;

    //seleccion de imagen Constans
    private static final int IMAGE_PICK_CAMERA_CODE = 102;
    private static final int IMAGE_PICK_GALLERY_CODE = 103;

    //matrices de permisos
    private String[]cameraPermissions; ///camara y almacenamiento
    private String[]storagePermissions;//solo almacenamiento

    //variables  (constain de datos para guardar)
    private Uri imagePlaca, imageContenedor, imageSello;
    private String id, fechaIngreso,placa,nombreaudit, addedTime, updatedTime;

    private boolean isEditMode = false;

    //db helper

    private DBHelper dbHelper;

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

        //inicialización
        actionBar = getSupportActionBar();
        //Titulo
        actionBar.setTitle("Agregar Registro");
        //Boton
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowHomeEnabled(true);

        fechaIngresoEt = findViewById(R.id.txt_FechaIngreso);
        placaEt = findViewById(R.id.txt_Placa);
        nombreAuditEt = findViewById(R.id.txt_NombreAudit);
        imgPlaca = findViewById(R.id.btn_FotoPlaca);
        imgContenedor = findViewById(R.id.btn_FotoContenedor);
        imgSello = findViewById(R.id.btn_FotoSello);
        Guardar = findViewById(R.id.btn_Guardar);
    }

 private void imagePickDialog() {
        //opciones para mostrar en el diálogo
        String[] options = {"Cámara", "Galería"};
        //diaolgo
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //Título
        builder.setTitle("Seleccione la imagen");
        //establecer elementos / opciones
        builder.setItems(options, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //manejar Clicks
                if (which == 0) {
                    if (!checkCameraPermission()) {
                        requestCameraPermission();
                    } else {
                        //permiso ya otorgado
                        PickFromCamera();
                    }
                } else if (which == 1) {
                    if (!checkStoragePermission()) {
                        requestStoragePermission();
                    } else {
                        //permiso ya otorgado
                        PickFromGallery();
                    }
                }
            }

        });
        //Crear /mostrar diálogo
        builder.create().show();
    }

private void PickFromCamera() {
        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.TITLE, "Título de la imagen");
        values.put(MediaStore.Images.Media.DESCRIPTION,"Descripción de la imagen");
        //poner image URI
        imagePlaca = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        imageContenedor = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        imageSello = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

        //intento de abrir la cámara para la foto
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imagePlaca);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageContenedor);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageSello);
        startActivityForResult(cameraIntent, IMAGE_PICK_CAMERA_CODE);
    }

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //la imagen seleccionada de la cámara o la galería se recibirá
        if (resultCode == RESULT_OK){
            //Image is picked
            if(requestCode == IMAGE_PICK_GALLERY_CODE){
                //Picked from gallery

                //crop image
                CropImage.activity(data.getData())
                        .setGuidelines(CropImageView.Guidelines.ON)
                        .setAspectRatio(1, 1)
                        .start(this);

            }
            else if(requestCode == IMAGE_PICK_CAMERA_CODE){
                //Picked from camera
                //crop Image
                //aqui es cunado selecciona la imagen para recortarla
                CropImage.activity(imagePlaca)
                        .setGuidelines(CropImageView.Guidelines.ON)
                        .setAspectRatio(1, 1)
                        .start(this);

            }
            else if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE){
                //aqui es cuando recorta la imagen y la almacena
                CropImage.ActivityResult result = CropImage.getActivityResult(data);
                if (resultCode == RESULT_OK){
                    Uri resultUri = result.getUri();
                    imagePlaca = resultUri;
                    //set Image
                    imgPlaca.setImageURI(resultUri);
                }
                else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE){
                    //ERROR
                    Exception error = result.getError();
                    Toast.makeText(this, ""+error, Toast.LENGTH_SHORT).show();
                }

            }

        }

        super.onActivityResult(requestCode, resultCode, data);
    }
}



0 Answers
Related