"java.lang.IllegalStateException: not connected to MediaScannerService" when adding a file to the media library in Android -
i'm receiving java.lang.illegalstateexception: not connected mediascannerservice exception in crash reports app.
they not many, don’t know what’s wrong in code, because on phone/emulator works ok. i’m using method call mediascanner adapted question/answer @ how , set (change) id3 tag (metadata) of audio files?
the method:
public static void scanmedia(context context, final file[] file, final string[] mime) { msc = new mediascannerconnection(context, new mediascannerconnectionclient() { public void onscancompleted(string path, uri uri) { utils.logger("d", "scanned " + path + ":", debug_tag); utils.logger("d", "-> uri: " + uri, debug_tag); msc.disconnect(); } public void onmediascannerconnected() { (int = 0; < file.length; i++) { msc.scanfile(file[i].getabsolutepath(), mime[i]); } } }); msc.connect(); } my calls:
utils.scanmedia(getapplicationcontext(), new file[] {myvideo}, new string[] {"video/*"}); or
utils.scanmedia(getapplicationcontext(), new file[] {myothervideo, myaudio}, new string[] {"video/*", "audio/*"});` how can avoid exceptions?
it's race condition. iterating on multiple files in onmediascannerconnected() method. disconnect() connection use add files.
say have 3 files. file 1 starts , file 2 can call scanfile() without problems well. before call scanfile() third file, first 1 has been completed. android calls callback method onscancompleted(). , here calling disconnect() closing connection want use third file. third scanfile()call connection no longer valid!
this might happen, or not. depending on thread runs how fast , gets processing time in particular order. exceptions every , then.
i provide pull request fix ytdownloader if like.
Comments
Post a Comment