0

Does anybody know if you can save an ImageIcon file with ObjectOutputStream? I have a person-registry that I save with an ObjectOutputStream, So it would be really convenient to to write the the imageicon together with that information. But when I try to read the image icon, nothing happens. I get to read all the other information but cant seem to get the ImageIcon.

Anybody got any ideas to what I can do?

EDIT: I have a person class and a person registry class. below is the person class and the personregistry class is just a collection of several persons. i write out the person-registry with objectoutput stream.

public Person(String fn, String en, String a, int t, ImageIcon kb, Kort k)   {
        fname= fn;
        lname= en;
        adress= a;
        tlf= t;
        card= k;
        df.format(medlemsNr= ++nextNr);
        cardPicture= kb;
        neste= null;
    }

public void writePersonsToFile()   {
        try(ObjectOutputStream utfil= new ObjectOutputStream(
                new FileOutputStream(("PersonRegistry.data"))))   {
            utfil.writeObject(personRegistry);
        }
        catch(NotSerializableException nse) {

        }
        catch(IOException ioe)  {

        }
    }
fuLLMetaLMan
  • 165
  • 4
  • 17
  • What happens when you read the `imageicon` with `ObjectInputStream` ? – Apurv Apr 28 '13 at 13:44
  • Well, nothing. First I read the person-registry with succes. All the data (text) is there, but the imageicon I want it to display on a JLabel with JLabel.setIcon(imageIcon), but nothing happens. – fuLLMetaLMan Apr 28 '13 at 13:48
  • Can you share the code where you are saving `imageicon` with `ObjectOutputStream` and the code where you are reading `imageicon` – Apurv Apr 28 '13 at 14:37
  • posted code. And i use the same method when I read. InputObjectStream personRegistry= (Personregistry) inFile.readObject(); – fuLLMetaLMan Apr 28 '13 at 15:09

2 Answers2

1

I don't understand your question? You have a stream of image and you want to create a imageicon object to draw or other action?

If this, you can using ImageIO.read(ImageInputStream stream)in ImageIO class

If you have a stream of image (ex: from url) and want to save to image file, you can save to file by wrap it with FileOutputStream and simple write it to a file.

5agado
  • 2,444
  • 2
  • 21
  • 30
  • The thing is I have a ImageIcon and I want to save it together with String information (if this is possible) in an objectoutputstream and then read it in again from an objectinputstream. Or do I have to save the imageicon seperately in an ImageOutput? – fuLLMetaLMan Apr 28 '13 at 15:12
1

I solved it at last. It was a stupid mistake on my hand. It worked fine to save the ImageIcon through ObjectOutputStream and in again. The trouble was with the file I tried to save.

The ImageIcon that I wanted to save was inside a JLabel on the GUI. To save it, I had to get the Icon first with:

ImageIcon imgIcon= (ImageIcon)imgLabel.getIcon();

and then run it through the outputstream. Thanks for all help. =)

Apurv
  • 3,723
  • 3
  • 30
  • 51
fuLLMetaLMan
  • 165
  • 4
  • 17