Image for Base64

Asked

Viewed 64 times

0

I would like to turn an image to Base64, but it is not working properly, there is no error in logcat,anyone could help me?

Code:

 private void galleryIntent() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);//
    startActivityForResult(Intent.createChooser(intent, "Select File"), SELECT_FILE);
}

private void cameraIntent() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, REQUEST_CAMERA);
}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == SELECT_FILE)
            onSelectFromGalleryResult(data);
        else if (requestCode == REQUEST_CAMERA)
            onCaptureImageResult(data);
    }
}

public void onCaptureImageResult(Intent data) {


    Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] imageBytes = baos.toByteArray();

    String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);




    File destination = new File(Environment.getExternalStorageDirectory(),
            System.currentTimeMillis() + ".jpg");

                                                                                                                                                  FileOutputStream fo;
                            try {
                                destination.createNewFile();
                                fo = new FileOutputStream(destination);
                                fo.write(baos.toByteArray());
                                fo.close();
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                            livview.setImageBitmap(thumbnail);
                        }


@SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {

    Bitmap bm = null;
    if (data != null) {
        try {
            bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    livview.setImageBitmap(bm);
}
  • You can detail what "is not working properly" ?

  • I would like him to return me the Base64 as in systemoutprintln ...but it does not return anything

  • Even after placing Return imageEncoded;

1 answer

0

I managed to load the image in Base64 format using jquery with ajax, follow the code below

<script type="text/javascript">
    function uploadFile() {
        var target = document.querySelector("img");
        var file = document.querySelector("input[type=file]").files[0];
        
        var reader = new FileReader();
        
        reader.onloadend = function () {
            
            target.src = reader.result;
            
            // Upload Ajax
            $.ajax({
                method: "POST",
                url: "VisitanteServlet",
                data: { 
                    foto: reader.result
                }
            })
            .done(function(response) {
                //alert("Sucesso: " + response);
            })
            .fail(function(xhr, status, errorThrown) {
                //alert("Error: " + xhr.responseText);
            });
        };
        
        if (file) {                     
            reader.readAsDataURL(file);             
        } else {
            target.src = "";
        }
    }

</script>

Browser other questions tagged

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