Convert URI to Bitmap

0

Good afternoon,

I am a doubt, how can I convert a URI to Bitmap and then convert to Base64 so I can send it to the Database.

private void abrirCamera() {
    fileUri = getOutputPictureUri("GRP");

    if (fileUri != null) {

        // Cria o intent para chamar a câmara
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // seta o caminho do arquivo para guardar a foto
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

        // inicia a captura da foto
        startActivityForResult(intent, CAM_REQUEST);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAM_REQUEST) {
        if (resultCode == RESULT_OK) {

          Foto1.setImageURI(fileUri);
            }
        }
    }
    
asked by anonymous 04.11.2018 / 19:49

1 answer

0
Uri imageUri = data.getData();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();  
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();

//BASE64
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
    
05.11.2018 / 19:46