我正在开发一个应用程序,它需要在某个时刻截取屏幕截图并将其保存在文件系统中。我的问题是,在设备重置之前,图像在文件资源管理器中不可见,并且在某些型号中,图像甚至不显示,它只是一个不可读的 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");
}
}