Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to Read Base64 encode image file remotely

Tags:

android

base64

I have a image file which I uploaded to server using Base64 encoding(By converting to string). The server stored that string in a text file and gave me the url to that text file.

Can anybody guide me for, how can I get the encoded string from that text file remotely?

like image 860
mohitum Avatar asked Mar 16 '26 01:03

mohitum


1 Answers

Use this to decode/encode (only Java way)

public static BufferedImage decodeToImage(String imageString) {

    BufferedImage image = null;
    byte[] imageByte;
    try {
        BASE64Decoder decoder = new BASE64Decoder();
        imageByte = decoder.decodeBuffer(imageString);
        ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
        image = ImageIO.read(bis);
        bis.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return image;
}

public static String encodeToString(BufferedImage image, String type) {
    String imageString = null;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    try {
        ImageIO.write(image, type, bos);
        byte[] imageBytes = bos.toByteArray();

        BASE64Encoder encoder = new BASE64Encoder();
        imageString = encoder.encode(imageBytes);

        bos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return imageString;
}

Hope it's help

Update

Android way

To get image from Base64 string use

byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

UPDATE2

For reading text file from server, use this:

try {
    URL url = new URL("example.com/example.txt");
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    String str;
    while ((str = in.readLine()) != null) {
        // str is one line of text; readLine() strips the newline character(s)
    }
    in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}

And next time try to ask correct.

like image 138
jimpanzer Avatar answered Mar 18 '26 15:03

jimpanzer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!