File.renameTo fails when used with MediaTracker.

// The following code fails to rename the file

String fileName ="1.jpg";

String uploadedFile =new File("c:/uploaded", fileName);

Image image = Toolkit.getDefaultToolkit().getImage(uploadedFile.toString());

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

mediaTracker.addImage(image, 0);

mediaTracker.waitForID(0);

// If the uploaded image is a highrespic, move it to the highres folder

if (image.getWidth(null) > MAX_FOTO_WIDTH || image.getHeight(null) > MAX_FOTO_HEIGHT){

mediaTracker.removeImage(image, 0);

image =null;

mediaTracker.waitForID(0);

File highResFile =new File("c:/highres", fileName);

if (!uploadedFile.renameTo(highResFile)){

thrownew Exception("Couldn't rename the file!");

}

}

// The following code works!

String fileName ="1.jpg";

String uploadedFile =new File("c:/uploaded", fileName);

File highResFile =new File("c:/highres", fileName);

if (!uploadedFile.renameTo(highResFile)){

thrownew Exception("Couldn't rename the file!");

}

I would like to check the height and width of the image file before moving it. Can anyone please help me figure this out!

Thanks for your help!

Joe.

Message was edited by:

Joe12

Message was edited by:

Joe12

[2311 byte] By [Joe12a] at [2007-10-2 20:15:39]
# 1

Hi,

As you are running under windows, the file is locked by the system, you must wait that te GC close the file.

A call to System.gc();

might help.

If not you'll have to load your image with another system : in this case I use JAI.create(SeekableStream) and manage my stream myself since JAI.create(String fileName) has the same problem that thet ToolKit.

--Marc (http://jnative.sf.net)

mdentya at 2007-7-13 22:57:58 > top of Java-index,Java HotSpot Virtual Machine,Specifications...