Search code examples
blackberrysd-cardoverwrite

Why is this code not working? It won't overwrite the file for some reason


I'm using the following code to write an image to the sdcard and then display as wallpaper but it will only work once, that is, it won't change it for any other picture so I guess it isn't overwriting the file, why?

D.kF[] is a Bitmap array D.bgNumber is an int

Graphics graphics = Graphics.create(D.kF[D.bgNumber]);  

JPEGEncodedImage encoder = JPEGEncodedImage.encode(D.kF[D.bgNumber], 100);  
try {  
    ExtendedFileConnection fconn = (ExtendedFileConnection) Connector  
    .open("file:///store/home/user/pictures/image.jpg",  
            Connector.READ_WRITE);  
    fconn.enableDRMForwardLock();  
    if (!fconn.exists()) {  
        fconn.create();  
    }  
    OutputStream os = fconn.openOutputStream();  
    os.write(encoder.getData());  
    os.close();  
    fconn.close();  
} catch (Exception e) {  
    System.out.println("Output file error: " + e.getMessage());  
}  

HomeScreen.setBackgroundImage("file:///store/home/user/pictures/image.jpg");

Solution

  • I think there's a caching issue. Your image file has been changed, but wallpaper does not, because it uses cached old image.

    To overcome this in your code delete old image file, generate new image file with new different name and set background using this new file.