How to know when will the database update finish after MediaScanner?
-
17-04-2021 - |
Question
I have been working on an app which includes using the devices camera. After I take the image and save it on the SD card, I want to get it's Thumbnail through the Media Database of android. So, I have to start the MediaScanner to let the system to aware my image, which I did.
But, after the MediaScannerConnectionClient
finshed scan ,which can be known here
@Override
public void onScanCompleted(String path, Uri uri) {
if (mediaScanConn.isConnected()) {
mediaScanConn.disconnect();
}
}`
It seems that the database update is not finishing the right way, it takes about 800ms on my phone to finish the process, How can I know when the database has finished updating? Please help me,it will be best if you would post your code, Thanks a lot!
Solution
Even if inserting of the picture information to the database is already finished (same with scanning directory), you can not still guarantee that thumbnail creation is also finished. Thumbnail creation is actually running on a different thread because sometimes it takes some time to extract/decode the thumbnail from the file especially for video files.
When I checked the Android Camera Application source code, it seems that it is not waiting for the thumbnail generated when inserting the contents information to the database, instead they do the thumbnail generation inside the application. This is because, if the user take a picture when the MediaScanner is still running in the background (right after booting up or mounting of SD card), it will be placed in the end of the queue, which means that it will take time before your image will be processed.