0

I'm trying load an image in a string and after do something with this String, save the image.

The problem appear when i try to asignate the value of the FileInputStream to the String targetFileStr. If i don't to this, and I save the image, everything it's ok, but when i save it on the String, the image change, no matter if I try save the image from the String or from the FileInputStream.

    FileInputStream fis = null;

    File file = new File("image.png");
    fis = new FileInputStream(file);
    String targetFileStr = IOUtils.toString(fis, "UTF-8");

    *InputStream inputStream =  IOUtils.toInputStream(targetFileStr, "UTF-8");
    *InputStream inputStream = fis;
    // no matter which one i use, both ways fail

    OutputStream outputStream = null;

    try {

        outputStream = new FileOutputStream(new File("image2.png"));

        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = inputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

1 Answers1

2

You may want to consider converting the image into a String via Base64 encoding/decoding. This is an example of encoding.

After encoding, you can modify the String (actually you create new strings, you cannot modify the existing one), but be sure to produce valide base64-encoded outputs, otherwise you won't be able to decode.

Community
  • 1
  • 1
Manu
  • 4,019
  • 8
  • 50
  • 94
  • Thank you, I'll see what you told me. on the other hand I have been converting the string to byte [] and making operations needed there, but it seems easier with base64 – Alejandroppir May 26 '16 at 18:41