Android Open Source - Resonos-Android-Framework Media Scanner Notifier






From Project

Back to project page Resonos-Android-Framework.

License

The source code is released under:

Apache License

If you think the Android project Resonos-Android-Framework listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.resonos.apps.library.media;
/*from w  w w .ja v  a2 s. com*/
import android.content.Context;
import android.media.MediaScannerConnection;
import android.media.MediaScannerConnection.MediaScannerConnectionClient;
import android.net.Uri;
import android.os.Handler;
import android.provider.MediaStore;

/**
 * This class is used to notify the Android Gallery of new pictures we should
 * add to it.
 */
public class MediaScannerNotifier implements MediaScannerConnectionClient {
  private Context mContext;
  private Handler mHandler;
  private MediaScannerConnection mConnection;
  private String mPath;
  private String mMimeType;
  private MediaScannerNotifierListener mCallback;

  public interface MediaScannerNotifierListener {
    public void ready(String path, Uri uri);
  }

  private MediaScannerNotifier(Context context, Handler handler, String path,
      String mimeType, MediaScannerNotifierListener callback) {
    mContext = context;
    mPath = path;
    mMimeType = mimeType;
    mConnection = new MediaScannerConnection(mContext, this);
    mConnection.connect();
    mHandler = handler;
    mCallback = callback;
  }

  /**
   * Run the MediaScanner using an image file path
   * @param context
   * @param handler: any new handler will suffice
   * @param filePath
   * @param callback to see when the image is ready, optional
   * @return the MediaScannerNotifier object
   */
  public static MediaScannerNotifier run(Context context, Handler handler,
      String filePath, MediaScannerNotifierListener callback) {
    try {
      return new MediaScannerNotifier(context, handler, filePath,
          "image/jpeg", callback);
    } catch (Exception e) {
      // e.printStackTrace();
    }
    return null;
  }

  /**
   * Run the MediaScanner using an image URI
   * @param context
   * @param handler: any new handler will suffice
   * @param imgUri
   * @param callback to see when the image is ready, optional
   * @return the MediaScannerNotifier object
   */
  public static MediaScannerNotifier run(Context context, Handler handler,
      Uri imgUri, MediaScannerNotifierListener callback) {
    android.database.Cursor cur = null;
    try {
      cur = context.getContentResolver().query(imgUri,
          new String[] { MediaStore.Images.ImageColumns.DATA }, null,
          null, null);
      String filePath = null;
      if (cur != null && cur.moveToNext()) {
        filePath = cur.getString(0);
      }
      if (filePath != null) {
        return new MediaScannerNotifier(context, handler, filePath,
            "image/jpeg", callback);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (cur != null) {
        cur.close();
      }
    }
    return null;
  }

  @Override
  public void onMediaScannerConnected() {
    mConnection.scanFile(mPath, mMimeType);
  }

  @Override
  public void onScanCompleted(final String path, final Uri uri) {
    try {
      if (mCallback != null) {
        mHandler.post(new Runnable() {
          public void run() {
            mCallback.ready(path, uri);
          }
        });
      }
    } finally {
      mConnection.disconnect();
      mContext = null;
    }
  }
}




Java Source Code List

com.resonos.apps.library.Action.java
com.resonos.apps.library.AlertFragment.java
com.resonos.apps.library.App.java
com.resonos.apps.library.BaseFragment.java
com.resonos.apps.library.FragmentBaseActivity.java
com.resonos.apps.library.file.AltAndroidFileHandle.java
com.resonos.apps.library.file.AltAndroidFiles.java
com.resonos.apps.library.file.AltFileHandle.java
com.resonos.apps.library.file.FileCache.java
com.resonos.apps.library.media.AudioVisualizer.java
com.resonos.apps.library.media.BitmapMemoryCache.java
com.resonos.apps.library.media.HueColorFilter.java
com.resonos.apps.library.media.ImageLoader.java
com.resonos.apps.library.media.MediaScannerNotifier.java
com.resonos.apps.library.model.Coord.java
com.resonos.apps.library.model.ImmutableCoord.java
com.resonos.apps.library.tabviewpager.CustomViewPager.java
com.resonos.apps.library.tabviewpager.PageIndicator.java
com.resonos.apps.library.tabviewpager.TabPageIndicator.java
com.resonos.apps.library.tabviewpager.TabViewPagerAdapter.java
com.resonos.apps.library.tabviewpager.TabViewPagerFragment.java
com.resonos.apps.library.tabviewpager.TitleProvider.java
com.resonos.apps.library.util.AppUtils.java
com.resonos.apps.library.util.ErrorReporter.java
com.resonos.apps.library.util.LifecycleTaskQueue.java
com.resonos.apps.library.util.M.java
com.resonos.apps.library.util.NetworkClient.java
com.resonos.apps.library.util.NetworkRequest.java
com.resonos.apps.library.util.ParameterList.java
com.resonos.apps.library.util.SensorReader.java
com.resonos.apps.library.util.TouchViewWorker.java
com.resonos.apps.library.util.ViewServer.java
com.resonos.apps.library.widget.DashboardLayout.java
com.resonos.apps.library.widget.FormBuilder.java
com.resonos.apps.library.widget.FormElement.java
com.resonos.apps.library.widget.ListFormBuilder.java
com.resonos.apps.library.widget.PopupWindows3D.java
com.resonos.apps.library.widget.QuickAction3D.java
com.resonos.apps.library.widget.RangeSeekBar.java
com.resonos.apps.library.widget.SeekBar.java
com.resonos.apps.library.widget.ToolBarButton.java
com.resonos.apps.library.widget.ToolBar.java