Bastian
Bastian

Reputation: 101

Cannot decode BASE64 to Image file in Selenium 2

I have a BASE64 image from a screenshot capture that I would like to convert in a image.png file but there are compilation errors that I cannot understand.

    Object ss = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BASE64);
    Base64 decoder = new Base64();
    Byte[] imgBytes = (Byte[]) decoder.decode(ss);
    FileOutputStream osf = new FileOutputStream(new File("./screenshots/"+ System.getProperty("logfilename") + ".png"));        
    osf.write(imgBytes);
    osf.flush();

The compilation error in the code above is in the line osf.write(imgBytes) where it tells me that write() method will not accept Byte[] object because it tells me that the method only accepts write(int i) args. It is weird because when you see the options there is a write(Byte[] b).

Could you help me solve this riddle? Thanks

Upvotes: 1

Views: 1890

Answers (2)

Mike Kwan
Mike Kwan

Reputation: 24477

If you just want to save it as a file, you should just use the OutputFile.FILE enum. That essentially wraps the decoding for you.

Upvotes: 3

niharika_neo
niharika_neo

Reputation: 8531

The write func takes the primitive byte and not the wrapper class i.e. byte[] and not Byte[]. So the compiler error..

Upvotes: 1

Related Questions