2012-09-28 121 views
0

我製作了一個保存系統,我保存圖片,然後關閉應用程序,然後查找文件夾。我想打開圖片,但圖片打開得很慢。當我重置三星galaxyw電話時,這張照片打開速度非常快(1s)。我的代碼有什麼問題?保存並打開圖片

private String mImagePath = Environment.getExternalStorageDirectory() + "/anppp"; 
private File file; 

public void save() { 

    File dirPath = new File(Environment.getExternalStorageDirectory().toString() + "/anppp"); 
    dirPath.mkdirs(); 
    Calendar currentDate = Calendar.getInstance(); 
    SimpleDateFormat formatter= new SimpleDateFormat("yyyyMMMddHmmss"); 
    String dateNow = formatter.format(currentDate.getTime()); 
    file = new File(mImagePath + "/"+"ProPaint-" + dateNow +".jpg"); 
    FileOutputStream fos=null; 


    try { 

     if(!file.exists()) 
      file.createNewFile(); 

     fos = new FileOutputStream(file); 
     mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); 
     fos.close(); 

    } catch (FileNotFoundException e) { 
     Log.e("Panel", "FileNotFoundException", e); 
    } catch (IOException e) { 
     Log.e("Panel", "IOEception", e); 
    } 

} 
+0

你能否提供你的錯誤logcat。我們需要更多信息來幫助您解決問題。 – al23dev

+0

可能與大圖像有關,並且還沒有創建拇指 – njzk2

+0

09-28 15:29:20.161:W/KeyCharacterMap(1173):使用默認鍵盤映射:/system/usr/keychars/qwerty.kcm.bin – mamii

回答

0

不知道這是否會真正解決您的問題,但你應該在fos.close()呼叫轉移到一個finally塊,以確保其執行。如果fos.close()以上的某行發生異常,則以下代碼將被忽略,並且FileOutputStream將保持打開狀態,給您帶來各種麻煩。當它位於finally塊中時,無論情況如何,它都會執行。希望這可以幫助。

+0

未處理的異常類型IOException fos.close();必須在嘗試;(((不在最後).. – mamii

+0

@mamii,它應該在最後,也包圍了try-catch塊 – Egor

+0

我不能顯示代碼如何改變..;(但它stil打開圖片8秒我不知道什麼樣的變化,當我重新設置手機.. – mamii