我正在开发一个需要在特定时刻截取屏幕截图并将其保存在文件系统中的应用程序。我的问题是在重置设备之前图像在文件浏览器中不可见,并且在某些模型中,图像甚至不显示,它只是一个不可读的img文件(如曲线)。
拍摄照片的代码是:
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");
}
}
答案 0 :(得分:1)
我没试过。但是为什么要为png文件生成.jpg扩展名?
正确答案是:输出流未正确关闭。