File not showing changes inside JVM

Hi all,

I wrote a Java app that lists jpeg files as thumbnails and allows modifications such as rotation, cropping...

When I save the changes (let's say a 90 degrees clockwise rotation) I made to one file inside my app and reload the same file in my app's list the changes (i.e. the rotation) are all gone away. The file is always the same as before making any changes to it.

But if I open that file inside another app (Photoshop or The Gimp or whatever) the changes (rotation) are there!

I encounter the same issue if I open a picture inside my app and save it after modifying it outside (in any picture editing tool). If I reload the same picture in my app no modifications are visible at all while they are in any other picture viewer/editor.

I see the same behavior with my app under Mac OS X 10.3 and 10.5 (on PPC and Intel), Windows 9x/XP and Linux (Mandriva 10.2 and Ubuntu 6.06), using jdk 1.5_06.

If I quit my app and lauch it again the file shows its changes (the rotation here)...

What is going on? It seems that the JVM is using some kind of caching for files.

Is it normal or am I doing something wrong?

Thanks in advance for any help.

Regards,

Lara.

[1238 byte] By [JolieLaraa] at [2007-11-27 5:15:29]
# 1
Did you actually close the stream after saving?
CeciNEstPasUnProgrammeura at 2007-7-12 10:37:45 > top of Java-index,Java Essentials,Java Programming...
# 2

Yes I did.

I even tried to sync the corresponding FileDescriptor without any success...

And to open file I use the Toolkit:

Image image = Toolkit.getDefaultToolkit().getImage(szFilePath);

MediaTracker mediaTracker = new MediaTracker(new Container());

mediaTracker.addImage(image, 0);

mediaTracker.waitForID(0);

mediaTracker.removeImage(image, 0);

where szFilePath is a String containing the full path to the corresponding file.

If I modify the file outside of my app, save the changes and reload the same file using the code above no changes are visible...

Here is a simple class to show the issue:

just use "javac Cache.java" and "java Cache", click on the "..." button to open a picture. It should be displayed in the center. Then modify the same picture (without closing Cache ) inside a picture editing app ('toshop or other) save it then go back to Cache and click again on "..." to choose the same file: you won't see any changes!

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class Cache

{

public static void main(String[] args)

{

CacheFrame f = new CacheFrame();

f.show();

}

}

class CacheFrame extends Frame implements ActionListener

{

private PictureCanvas m_picture;

public CacheFrame()

{

m_picture = new PictureCanvas();

Dimension dim = new Dimension(640, 480);

setSize(dim.width, dim.height);

setLayout(new BorderLayout());

setBackground(Color.white);

add(m_picture, "Center");

Button b = new Button("...");

b.addActionListener(this);

add(b, "South");

addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

});

}

public void actionPerformed(ActionEvent e)

{

String szPath = new String(System.getProperty("user.dir"));

FileDialog fileDialog = new FileDialog(this, szPath, FileDialog.LOAD);

fileDialog.setDirectory(szPath);

fileDialog.setVisible(true);

String szDirectory = fileDialog.getDirectory();

String szFile = fileDialog.getFile();

fileDialog.dispose();

if(null!=szDirectory&&null!=szFile)

{

szPath = szDirectory + szFile;

m_picture.setPicture(szPath);

}

}

public void setPictureSize(int nSize)

{

m_picture.setPictureSize((double)nSize);

}

}

class PictureCanvas extends Canvas

{

private Image m_picture;

private double m_dCurrentSize = 1.00;

private int m_nPictWidth, m_nPictHeight;

public PictureCanvas()

{

}

public void setPicture(String szFilePath)

{

if(null!=szFilePath&&0<szFilePath.length())

{

try

{

Image image = Toolkit.getDefaultToolkit().getImage(szFilePath);

MediaTracker mediaTracker = new MediaTracker(new Container());

mediaTracker.addImage(image, 0);

mediaTracker.waitForID(0);

mediaTracker.removeImage(image, 0);

m_picture = image;

}

catch(InterruptedException e)

{

}

if(null!=m_picture)

{

m_nPictWidth = m_picture.getWidth(null);

m_nPictHeight = m_picture.getHeight(null);

repaint();

}

}

}

public void paint(Graphics g)

{

if(null!=m_picture)

{

Dimension d = getSize();

int nW = (int)(m_dCurrentSize*m_nPictWidth);

int nH = (int)(m_dCurrentSize*m_nPictHeight);

int nX = d.width/2 - nW/2;

int nY = d.height/2 - nH/2;

g.drawImage(m_picture, nX, nY, nW, nH, this);

}

}

public void setPictureSize(double dSize)

{

m_dCurrentSize = dSize;

repaint();

}

}

I'm stuck... :-(>

JolieLaraa at 2007-7-12 10:37:45 > top of Java-index,Java Essentials,Java Programming...
# 3
OK I found the solution:must use Toolkit.createImage instead of Toolkit..getImage in Image image = Toolkit.getDefaultToolkit().getImage(szFilePath); since getImage is meant to share data among different callers... :-DRegards,Lara.
JolieLaraa at 2007-7-12 10:37:45 > top of Java-index,Java Essentials,Java Programming...