I'm making a program using java that sends the clipboard contents over sockets; I managed to make it work with strings but I'm having some troubles with images. Here is the code:
//get Image
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Image imageContents = (Image)clipboard.getData(DataFlavor.imageFlavor);
ImageIcon image = new ImageIcon(imageContents);
//sent over sockets
//set Image
String mime = DataFlavor.imageFlavor.getMimeType();
DataHandler contents = new DataHandler(image,mime);
//set clipboard
clipboard.setContents(contents, null);
After setContents the clipboard is empty; Any ideas why, and how to fix it?
Here is some code I've used to write/read an Image from the clipboard. Never tried it with sockets so I'm not sure it will help:
DataHandler will not function in the way you've prescribed because according to the API:
I understand this to mean that unless you use its
setDataContentHandlerFactory
method and implement all required interfaces therein, essentially, DataHandler will simply return null. This is likely the reason why DataHandler does not function as you expect. Even though it implements theTransferable
interface, it does not implement it in a way that functions adequately for your particular needs.This functionality would be given by a
DataContentHandler
whose implementation would be left up to you.It seems less tedious to directly implement the Transferable class as suggested prior.