2014年5月13日 星期二

Can's see the newly created picture in Android's default gallery.

當我們寫了一個會儲存圖片在裝置上的 APP 時,卻在開啟 Android gallery 或其他專門
瀏覽圖片的 APP 時看不到圖片,是因為系統還沒將其掃進 MediaStore,想必如果系統
不斷的掃描檔案和操作資料庫是相當耗資源與沒有效率的。所以,Android 會在每次
開機時做一次掃描,如果 APP 想要系統對某個檔案做即時的掃描,可以加上下面的

程式碼:
MediaScannerConnection.scanFile(activity, new String[]{path}, null, 
    new MediaScannerConnection.OnScanCompletedListener() {
        @Override
        public void onScanCompleted(final String path, final Uri uri) {
           ...
        }
    }
); 

Ref: http://developer.android.com/reference/android/media/MediaScannerConnection.html

沒有留言:

張貼留言