I'm developing a desktop java application.
Each users have a panel showing personal information and also a profile image.
The image is added through Swing JFileChooser, stored in a folder and its path saved in a local database as a string.
Everything work, unless that the user have the possibility to change the image.
Changing the image use a jFileChooser again, and is the same as adding the image from the beginning. The new path is stored correctly, but there's a cache memory issue.
Problem is that images are stored in the hard drive with a file name that must be equal to the current user name. Changing image from the app will create a new image in that folder with the same name, overwriting the previous one.
Now:
- If i open the user panel again, the "old" image is shown, because new and old image path are identical and java use the cached image instead of reloading it
- Closing the app and restarting it will solve the problem, so the path is stored correctly in the database and the issue must be related to the cache memory.
Help me pls!
Thanks in advance for your replies
Related
I am making a Cluedo boardgame in java and I cannot get my images to show up when converting it to a .jar file.I am adding the images on top of one another so I used JLabels(new ImagIcon ("..")) like so.To add the images and now the images can't be seen when I create a .jar file. Just wondering if there is any way to do without editing my code a significant amount. I have read all stackoverflow answers on this issue and none of them solves this particular problem. Here is what the board game looks like when I run it in Eclipse.
When I convert it to .jar file it no longer displays the board or any of the players.
ImageIcon(String) expects that the String value is "file" reference to the image residing on disk.
If you have externalised your resources then you need to ensure that the path you are using is correct. Remember, the "working directory" in which the Jar is executed isn't always the same as the directory that the Jar is stored in.
To this end, it's generally recommend to "embedded" the images (and other resources) within the Jar itself. This way you can simply perform a lookup for the resource regardless of where it's installed.
The means by which you embedded resources is slightly different for each IDE, but it basically requires that the images are included in the Jar and a specific location/package.
An important note, when embedded in the Jar, the resources can no longer be referenced as if they "files", because they're not, they are part of the Jar/Zip archive and need to be referenced in a different manner.
To load embedded resources you need to use Class#getResource, which returns a URL or Class#getResourceAsStream which returns a InputStream.
In most case, the first is enough. While ImageIcon does take a URL, it is generally recommended to use ImageIO.read, have a read through Reading/Loading images for more details.
The advantage of this is two fold:
It blocks until the image is read, meaning the image is fully realised when the method returns
It throws an IOException when the image can't be read, which is way more meaningful, as ImageIcon fails silently
So, all that would accumulated down to something like...
new JLabel(new ImageIcon(ImageIO.read(instanceOfMyAwesomeGameObject.getResource("/path/to/resource/ResourceName.png"))));
I think it comes from the path to your images. The image path for sources is probably not the same as for .jar
I am a beginner Java developer and I have created an application in Java (Netbeans).
I have used buttons icons, backgrouds for jframes etc. When I built the project, they can easily accessible using WinRAR. Anyone can extract my jar file and see all the images that I have used in my program. Even the images used in jpanel that is not accessible without password.
How can I hide or protect these images?
I have created a .exe file also using Launch4j but still facing same problem. When I right click on the .exe file, its easy to extract my whole program.
To be honest, if someone wants your picture, they are going to be able to get it. One can simply do a print screen. However you can still do somethings.
Remove the extensions for the picture files and give them crazy names - this will make it harder for people to find your pictures
Encript the files then have the application decript them when loading - I don't know how to do this but shouldn't be too hard to find, for instance you could save the pictures as a byte stream with some alterations then reload it.
Save the picture in some archive which only your application knows how to read.
But anyway even with all these things, I still know how one could get a handle to an open JFrame, look through the components, and then could get your picture. IMHO trying to prevent people for getting your pictures is not worth doing.
I want to have an image as part of my apk that the user can modify using my app.
I dont want to save it on the SD card. It should not be viewable or editable outside the app.
Where do I need to put the original image, the one I include to be used by default?
In the resources drawble folder? Or the assets folder?
And how do I overwrite that image with a user-generated one? (with the idea that this will be used in the app everywhere instead.)
You cannot overwrite resources not assets contents.
Alternative solution:
What about saving it to the internal directory of an application, which would not be accessible outside of you application scope? and then every time you display a picture, check if the one in internal memory exists, if it does, then display that one instead of the one in drawables
docs: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
You can save files directly on the device's internal storage. By
default, files saved to the internal storage are private to your
application and other applications cannot access them (nor can the
user). When the user uninstalls your application, these files are
removed.
I have created a canvas(html5) element to let the user draw on it, after the drawing, they can save(a button) it: canvas.toDataURL("image/png"); will be
executed and the encoded base64 dataURL string will be stored to a file. But I need to let the user view his paintings.
Here is the question: As the user save many story strings in a text file and the filesystem read the it, the memory will leak.
PhoneGap complains: java.lang.OutOfMemoryError, because the user saved too much dataURL strings. As all of them have to be reloaded to an array, error happens.
I suppose that if I save the user's drawing in image format(.jpg) instead of base64 dataURL, will it work?
Will it solve the memory leak issue?
How can I retrieve the images which are stored in the path like: sdcard/story/pics/ ?
If I just want to access a recorded file, I can write:
var record= new Media(src,onSuccess, onError); where the src could just be: story/rec/something.mp3
But the I can't access the image like this way.
The solution should also work on iOS, because I have to deploy the app to iPad.
Develop Environment:
PhoneGap 1.8.0
Android 4.0.3
I ran into a similar problem with binary files, and the bad news is that you cannot write binary data to the file system, as binary data cannot be transferred across the bridge into the native code.
I ended up writing a plugin to do the binary manipulation (or rather three plugins, one for iOS, another for Android and a third for BlackBerry).
I'm not sure what is causing you memory leak, but I would suggest only having one drawing in memory at once, unless you allow copy and paste between images, in which case I'd limit the app to two images in memory at once.
Whatever the leak is, it would have to be severe to use up all available memory, as most modern Android devices have quite a bit of memory on them.
I'm using assets as standard icons storage but now I have request that these icons should be updated for some cases. I know that I can't touch assets, so do you have any suggestions where to store them?? These files should be pre-installed and updated for some cases. From start I've been thinking about Internal Storage but now I have some doubts. What do you think???
So the solution to this problem was to put current available images in assets. App checks is there anything new every time user logs in, if yes places new images in /data/../images folder. When icons are required, I first check assets and then the storage. In new app releases I include the new icons in the assets folder.