2013-07-20 22 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

像冠军一样工作谢谢 –

相关问题