I want to encode and save from an URL images in Base64. I found several example doing the encoding from a local file but not from an URL. Is there a possibility to do that?
I tried something like that, but unsuccessfully. Any clue,help? Thanks for your answer.
    public static void main(String[] args)  {
    String imageUrl = "http://www.avajava.com/images/avajavalogo.jpg";
    String destinationFile = "image.jpg";
    try {           
        // Reading a Image file from file system
        URL url = new URL(imageUrl);
        InputStream is = url.openStream();
        FileInputStream imageInFile = new FileInputStream(is.toString());
        byte imageData[] = new byte[2048];
        imageInFile.read(imageData);
        // Converting Image byte array into Base64 String
        String imageDataString = encodeImage(imageData);
        System.out.println("imageDataString : " + imageDataString);
        System.out.println("Image Successfully Manipulated!");
    } catch (FileNotFoundException e) {
        System.out.println("Image not found" + e);
    } catch (IOException ioe) {
        System.out.println("Exception while reading the Image " + ioe);
    }
}
/**
 * Encodes the byte array into base64 string
 *
 * @param imageByteArray - byte array
 * @return String a {@link java.lang.String}
 */
public static String encodeImage(byte[] imageByteArray) {
    return Base64.encodeBase64URLSafeString(imageByteArray);
}
                Base64 is a group of binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation. By consisting only of ASCII characters, base64 strings are generally url-safe, and that's why they can be used to encode data in Data URLs.
What is Base64? Base64 is a binary-to-text encoding scheme that represents binary data in a printable ASCII string format by translating it into a radix-64 representation. Each Base64 digit represents exactly 6 bits of binary data.
If we were to Base64 encode a string we would follow these steps: Take the ASCII value of each character in the string. Calculate the 8-bit binary equivalent of the ASCII values. Convert the 8-bit chunks into chunks of 6 bits by simply re-grouping the digits.
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;
}
                        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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With