2013-07-20 18 views
2

因此,在我的應用程序中,我保存了一張照片,然後出現在手機的圖庫中。我認爲它看起來很快,但它不是瞬間的,因爲它而越來越差。我已經看過應用程序,他們立即出現在畫廊中,我希望我也這樣做,以避免更糟糕的評論。我使用sendBroadcast,我認爲這是最快的方法,但我想我錯了。保存的圖像需要一段時間才能出現在圖庫中

public File savePhoto(File pic,String ext) 
{ 
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Pics"); 



    // Create the storage directory if it does not exist 
    if (!mediaStorageDir.exists()) 
    { 
     if (!mediaStorageDir.mkdirs()) return null; 
    } 

    // Create a media file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
    File mediaFile=null; 

    mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + "."+ext); 
    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Pics")))); 

    Toast.makeText(this, "Image Saved", Toast.LENGTH_SHORT).show(); 

    return mediaFile; 
} 

回答

1

我在這裏可能是錯的。 sendBroadcast()Intent.ACTION_MEDIA_MOUNTED是非常重要的,可能會造成延誤。

您可以嘗試使用在其位置如下:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(mediaFile))); 

我使用你剛纔上面的sendBroadcast()方法創建mediaFile。這應該會更好,因爲你只關注一個文件。

+0

像冠軍一樣工作謝謝 –

相關問題