Convert URI to Bitmap

Asked

Viewed 677 times

0

Good afternoon,

I am as a doubt, how can I convert a URI to Bitmap and then convert to Base64 so I can send 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);
            }
        }
    }

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);
  • It informs that the date is NULL, would have as someone to give me a light.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.