2012-07-23 128 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