title: Android: Problem with Media Scanner not Running
body: <p>I have an app that user's can draw with, and then 'export' that drawing as a .png file to external storage, if present.  Generating the PNG, copying the file to external all work like a charm, but a rather unique problem happens; after the export, if the user navigates to the image via My files (Samsung Tab running 2.2 in this case), they can see the .png file, but when they open it, the screen is black for about 10 seconds... then they see the image,  Additionally, the images don't show up in the user's 'Gallery' app either.</p>
  
  <p>Now, if the user connects the device to the computer via USB, or reboots the device, they can access the images no problem from My files, and they appear in 'Gallery' from that point forward, but again, any newly esported files experience the same problems until they cycle/connect the device again.</p>
  
  <p>My thinking was that this had to be related to the Media Scanner (at least in the case of the 'Gallery' problem, it most certainly is).</p>
  
  <p>So, as I am targetting Api 8+, I am trying to use the static <code>MediaScannerConnection.scanFile()</code> method to have the OS re-scan and add my images into the Gallery, etc.  Also hoping this solves the issue of the strange delay in opening the images.  Here is my code:</p>
  
  <pre><code>MediaScannerConnection.scanFile(
    context, 
    new String[] { "/mnt/sdcard/MyApp" }, 
    null, 
    null
  );
  </code></pre>
  
  <p>LogCat gives me the following entries when I export an image, and thus run the above call:</p>
  
  <pre><code>DEBUG/MediaScannerService(2567): IMediaScannerService.scanFile: /mnt/sdcard/MyApp mimeType: null
  DEBUG/MediaScannerService(2567): onStartCommand : intent - Intent { cmp=com.android.providers.media/.MediaScannerService (has extras) }
  DEBUG/MediaScannerService(2567): onStartCommand : flags [0], startId [1]
  DEBUG/MediaScannerService(2567): ServiceHandler:handleMessage volume[null], filePath[/mnt/sdcard/MyApp]
  DEBUG/MediaProvider(2567): getSdSerial() sd state = removed
  INFO/Database(2567): sqlite returned: error code = 17, msg = prepared statement aborts at 43: [SELECT DISTINCT sd_serial FROM images WHERE sd_serial LIKE 'external_0x%']
  ERROR/MediaProvider(2567): removeMediaDBData called
  DEBUG/MediaScanner(2567): prescan enter: path - /mnt/sdcard/MyApp
  DEBUG/MediaScanner(2567): prescan return
  </code></pre>
  
  <p>So, it looks like the MediaScanner is getting the correct location, but is failing to find the SD card, which is correct, and failing.  The Samsung Tab has built-in non-SD external storage, which Android gives access to via <code>Environment.getExternalStorageDirectory()</code>.  How do I tell it to scan the non-SD storage?</p>
  
  <p>Any ideas how to proceed?</p>
  
  <p>Paul</p>