title: media scanner running
body: <p>in my application user captures the images through camera functionality after that i am going to update the image in grid view .but those are not updated after long search i found need to run media scanner manually .i write code for that .but when i capturing images repeatedly   it gives error.below i posted my log cat errors....</p>
  
  <pre><code>11-05 17:17:17.565: E/AndroidRuntime(30801): FATAL EXCEPTION: main
  11-05 17:17:17.565: E/AndroidRuntime(30801): java.lang.IllegalStateException: not connected to MediaScannerService
  11-05 17:17:17.565: E/AndroidRuntime(30801):    at android.media.MediaScannerConnection.scanFile(MediaScannerConnection.java:164)
  11-05 17:17:17.565: E/AndroidRuntime(30801):    at com.imagedatabase.Category$6.onMediaScannerConnected(Category.java:512)
  11-05 17:17:17.565: E/AndroidRuntime(30801):    at android.media.MediaScannerConnection.onServiceConnected(MediaScannerConnection.java:249)
  11-05 17:17:17.565: E/AndroidRuntime(30801):    at android.app.ActivityThread$PackageInfo$ServiceDispatcher.doConnected(ActivityThread.java:1247)
  11-05 17:17:17.565: E/AndroidRuntime(30801):    at android.app.ActivityThread$PackageInfo$ServiceDispatcher$RunConnection.run(ActivityThread.java:1264)
  11-05 17:17:17.565: E/AndroidRuntime(30801):    at android.os.Handler.handleCallback(Handler.java:587)
  11-05 17:17:17.565: E/AndroidRuntime(30801):    at android.os.Handler.dispatchMessage(Handler.java:92)
  11-05 17:17:17.565: E/AndroidRuntime(30801):    at android.os.Looper.loop(Looper.java:123)
  11-05 17:17:17.565: E/AndroidRuntime(30801):    at android.app.ActivityThread.main(ActivityThread.java:4627)
  </code></pre>
  
  <p>below i posted the my code where i am going to run this one and i am explaining each and every step in comments.</p>
  
  <pre><code>    enter code here
  
     public class ButtonClickHandler implements View.OnClickListener {
      public void onClick(View view) {
  
          //when camera button is pressed it starts 
              startCameraActivity();
  
  
      }
  }
  
  
  
  
  public void startCameraActivity() {
  
         //seperating the images with date
  
  
  
  
      Date date = new Date();
      DateFormat df = new SimpleDateFormat("yyyy-MM-dd-kk-mm-ss");
      String newPicFile = df.format(date);
  
  
  
      direct = new File(Environment.getExternalStorageDirectory() + path
              + "/" + newPicFile + ".jpg");
  
  
  
      outputFileUri = Uri.fromFile(direct);
  
                             //here i am starting the  camra 
  
  
  
      Intent intent = new Intent(
              android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
  
      intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
  
      startActivityForResult(intent, 0);
  
  
  
  
     }
  
  
  
  
  enter code here
  
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
      Log.i("image data base", "resultCode: " + resultCode);
      switch (resultCode) {
      case 0:
                          //when user cancel the image take here i am deleted the urifile
  
  
          File file = new File(outputFileUri.getPath());
          boolean deleted = file.delete();
  
          Log.i("image data based", "User cancelled");
          break;
  
      case -1:
          onPhotoTaken();
  
          break;
      }
       }
  </code></pre>
  
  <p>//in this on photo taken i wrote the code for media scanner </p>
  
  <pre><code>enter code here
  protected void onPhotoTaken() {
  
      Log.i("MakeMachine", "onPhotoTaken");
  
      taken = true;
      SharedPreferences path_shared = Category.this.getSharedPreferences(
              "path", MODE_WORLD_WRITEABLE);
      imagePath = path_shared.getString("mypath", "0");
  
      File newFile = new File(imagePath);
      final Uri outputFileUri = Uri.fromFile(newFile);
  
      mScanner = new MediaScannerConnection(Category.this,
              new MediaScannerConnection.MediaScannerConnectionClient() {
                  public void onMediaScannerConnected() {
  
          mScanner.scanFile(outputFileUri.getPath(), null /* mimeType */);
                  }
  
                  public void onScanCompleted(String path, Uri uri) {
  
              if (path.equals(outputFileUri.getPath())) {
              mScanner.disconnect();
          // we need to create new UI thread because, we can't
      // update our mail thread from here
          // Both the thread will run one by one, see
                          // documentation of android
              Category.this.runOnUiThread(new Runnable() {
                              public void run() {
  
                              }
                          });
                      }
                  }
              });
      mScanner.connect();
                 }
  </code></pre>
  
  <p>i hope u got my point i also tried with </p>
  
  <pre><code>     sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://" + \
        outputFileUri+  Environment. getExternalStorageDirectory())));
  </code></pre>
  
  <p>but it some what slow i think it scan the entire sd card .so can any one please guide me how to run media scanner and also where can i run in my code.i searched lot but those are not satisfy my needs .</p>