2012-07-23 82 views
0

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

回答

1

我没有尝试。但是,为什么你为png文件生成.jpg扩展名?

正确的答案是:输出流未正确关闭。

+0

实际上,我已经有了解决方案,但它不让我在接下来的5个小时内发布它:p。我在最后关闭了outputStream – 2012-07-23 17:21:04