i am developing an application wich needs to take an screenshot at certain moment and save it in the file system. My problem is that the images are not visible in the file explorer until the device is reseted, and in some models, the image doesnt even show, its just an unreadable img file(like the curves).
My code to take the image is:
private Bitmap getScreenShot(){
Bitmap bitmap;
bitmap = new Bitmap(Display.getWidth(), Display.getHeight());
Display.screenshot(bitmap);
// return the screen shot
return bitmap;
}
private void saveInMemory(){
Bitmap screenShot = getScreenShot();
Date dateNow = new Date ();
SimpleDateFormat dateformatYYYYMMDD = new SimpleDateFormat("yyyyMMddHHmmss");
String timeStamp = dateformatYYYYMMDD.format(dateNow);
String mFileName = System.getProperty("fileconn.dir.photos")
+ "RM_" + timeStamp + ".jpg";
PNGEncodedImage png = PNGEncodedImage.encode(screenShot);
writeFile(png.getData(), mFileName);
}
private void writeFile(byte[] data, String fileName) {
FileConnection fconn = null;
try {
fconn = (FileConnection) Connector.open(fileName);
} catch (IOException e) {
System.out.print("Error opening file");
}
if (fconn.exists()){
try {
fconn.delete();
} catch (IOException e) {
System.out.print("Error deleting file");
}
}
try {
fconn.create();
} catch (IOException e) {
System.out.print("Error creating file");
}
OutputStream out = null;
try {
out = fconn.openOutputStream();
} catch (IOException e) {
System.out.print("Error opening output stream");
}
try {
out.write(data);
} catch (IOException e) {
System.out.print("Error writing to output stream");
}
try {
fconn.close();
} catch (IOException e) {
System.out.print("Error closing file");
}
}
I didn’t try. But why do you generate .jpg extension for png file?
The correct answer is: output stream wasn’t closed properly.