title: Observing changes in Android content observer for Audio.Media.EXTERNAL_CONTENT_URI
body: <p>I am developing an Android app in which I have to detect changes in Android SD card for audio files with the file name, file path and operation performed upon it. Example if I am adding a file in my SD card then I want to know</p>
  
  <ol>
  <li>Name of the file which is added</li>
  <li>Path of the file</li>
  <li>Operation  -- Add</li>
  </ol>
  
  <p>Previously I Have tried file observer But for that I have to apply it on each and every directory. So I searched for some other solution and got the info about <strong>Audio.Media.EXTERNAL_CONTENT_URI</strong>. Then I created a content observer like this</p>
  
  <p>UriObserver.java --  which is a content observer</p>
  
  <pre><code>class UriObserver extends ContentObserver {
  
      public UriObserver(Handler handler) {
          super(handler);
          // TODO Auto-generated constructor stub
      }
  
      @Override
      public void onChange(boolean selfChange) {
          // TODO Auto-generated method stub
          super.onChange(selfChange);
  
          Log.d("INSTANT", "GETTING CHANGES");
      }
  
  }
  </code></pre>
  
  <p>This is the code for registration for it</p>
  
  <pre><code>UriObserver observer = new UriObserver(new Handler());
  
  Log.d("INSTANT", "registered content observer");
  
  this.getApplicationContext()
      .getContentResolver()
      .registerContentObserver(
      MediaStore.Images.Media.EXTERNAL_CONTENT_URI, false,
      observer);
  
  Log.d("INSTANT", "registered content observer");
  </code></pre>
  
  <p>It let me know that some change has been occur  in sdcard related to audio files. But it doesn't gives any sort of info about which file has been added, edited or deleted.</p>
  
  <p>Then I searched for for solution and got this post</p>
  
  <p><a href="https://stackoverflow.com/questions/12546967/android-how-to-detect-a-change-in-mediastore-when-connected-over-mtp?answertab=active#tab-top">Android: How to detect a change in MediaStore when connected over MTP</a></p>
  
  <p>In this post some code is given by <strong>Bhiefer</strong> as an answer which I think it could work, so I tried to implement that but I am not able to do so.</p>
  
  <p>What can I do for this?</p>
  
  <h2>Update</h2>
  
  <p>Can I query Audio.Media.EXTERNAL_CONTENT_URI for its latest changes? This code:</p>
  
  <pre><code>mCursor = context.getContentResolver().query(
                      Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, "_id");
  
  mCursor.moveToLast();
  </code></pre>
  
  <p>doesn't give the latest changes. Is there any other method to get the latest changes?</p>