title: android using media scanner for SD card
body: <p>hi to all 
  im trying to save an image to an SDcard and im having a problem using the media scanner to scan the new saved file so that it is immediately available</p>
  
  <p>the errors are in the following lines </p>
  
  <pre><code>new String[] { file.toString() }, null, // error: file cannot be resolved
  
  new MediaScannerConnection.OnScanCompletedListener() { 
  // error: MediaScannerConnection.OnScanCompletedListener cannot be resolved to a type
  </code></pre>
  
  <p>this is my code:</p>
  
  <pre><code>public void saveToSDCard(Bitmap bitmap, String name) {
          boolean mExternalStorageAvailable = false;
          boolean mExternalStorageWriteable = false;
          String state = Environment.getExternalStorageState();
          if (Environment.MEDIA_MOUNTED.equals(state)) {
              mExternalStorageAvailable = mExternalStorageWriteable = true;
              Log.v(TAG, "SD Card is available for read and write "
                      + mExternalStorageAvailable + mExternalStorageWriteable);
              saveFile(bitmap, name);
          } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
              mExternalStorageAvailable = true;
              mExternalStorageWriteable = false;
              Log.v(TAG, "SD Card is available for read "
                      + mExternalStorageAvailable);
          } else {
              mExternalStorageAvailable = mExternalStorageWriteable = false;
              Log.v(TAG, "Please insert a SD Card to save your Ad "
                      + mExternalStorageAvailable + mExternalStorageWriteable);
          }
      }
  
      private void saveFile(Bitmap bitmap, String name) {
  
          String filename = name;
          ContentValues values = new ContentValues();
          File sdImageMainDirectory = new File(Environment
                  .getExternalStorageDirectory(), getResources().getString(
                  R.string.directory));
          sdImageMainDirectory.mkdirs();
          File outputFile = new File(sdImageMainDirectory, filename);
          values.put(MediaStore.MediaColumns.DATA, outputFile.toString());
          values.put(MediaStore.MediaColumns.TITLE, filename);
          values.put(MediaStore.MediaColumns.DATE_ADDED, System
                  .currentTimeMillis());
          values.put(MediaStore.MediaColumns.MIME_TYPE, "image/png");
          Uri uri = this.getContentResolver().insert(
                  android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
  
                  values);
          try {
              OutputStream outStream = this.getContentResolver()
                      .openOutputStream(uri);
              bitmap.compress(Bitmap.CompressFormat.PNG, 95, outStream);
  
              outStream.flush();
              outStream.close();
          // this is where im having the problem  
          // Tell the media scanner about the new file so that it is
              // immediately available to the user.
              MediaScannerConnection.scanFile(this,
                      new String[] { file.toString() }, null,
                      new MediaScannerConnection.OnScanCompletedListener() {
                  public void onScanCompleted(String path, Uri uri) {
                      Log.v("ExternalStorage", "Scanned " + path + ":");
                      Log.v("ExternalStorage", "-&gt; uri=" + uri);
                  }
              });
          } catch (IOException e) {
              // Unable to create file, likely because external storage is
              // not currently mounted.
              Log.v("ExternalStorage", "Error writing " + file, e);
          }
      }
  </code></pre>