Encode image from URL in Base64 in Java

Try this function by passing image url in parameter.

private String getByteArrayFromImageURL(String url) {

    try {
        URL imageUrl = new URL(url);
        URLConnection ucon = imageUrl.openConnection();
        InputStream is = ucon.getInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int read = 0;
        while ((read = is.read(buffer, 0, buffer.length)) != -1) {
            baos.write(buffer, 0, read);
        }
        baos.flush();
        return Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
    } catch (Exception e) {
        Log.d("Error", e.toString());
    }
    return null;
}

/**
     *
     * @param url - web url
     * @return - Base64 String
     * Method used to Convert URL to Base64 String
     */
    public String convertUrlToBase64(String url) {
        URL newurl;
        Bitmap bitmap;
        String base64 = "";
        try {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
            newurl = new URL(url);
            bitmap = BitmapFactory.decodeStream(newurl.openConnection().getInputStream());
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
            base64 = Base64.encodeToString(outputStream.toByteArray(), Base64.DEFAULT);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return base64;
    }

Following code converts image into the base64 string:

public String getBase64EncodedImage(String imageURL) throws IOException {
    java.net.URL url = new java.net.URL(imageURL); 
    InputStream is = url.openStream();  
    byte[] bytes = org.apache.commons.io.IOUtils.toByteArray(is); 
    return Base64.encodeBase64String(bytes);
}

P.S. Above code uses commons-io as a dependency.