Created
August 13, 2012 22:53
-
-
Save kmansoft/3344643 to your computer and use it in GitHub Desktop.
MediaScanner example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MediaScannerNotifier implements MediaScannerConnectionClient, Handler.Callback { | |
private static final String TAG = "MediaScannerNotifier"; | |
public static boolean submit(Context context, File file, String type) { | |
if (needScan(type)) { | |
new MediaScannerNotifier(context, file); | |
return true; | |
} | |
return false; | |
} | |
public static boolean submit(Activity activity, File file, Intent intent, OnViewErrorListener listener, | |
Object cookie) { | |
if (needScan(intent.getType())) { | |
new MediaScannerNotifier(activity, file, intent, listener, cookie); | |
return true; | |
} | |
return false; | |
} | |
public interface OnViewErrorListener { | |
public void OnViewError(Object cookie); | |
} | |
private static boolean needScan(String type) { | |
if (Build.VERSION.SDK_INT >= 11) { | |
return true; | |
} | |
return type != null && (type.startsWith("image") || type.startsWith("video")); | |
} | |
private MediaScannerNotifier(Context context, File file) { | |
mFile = file; | |
mContext = context.getApplicationContext(); | |
mConnection = new MediaScannerConnection(mContext, this); | |
connect(); | |
} | |
private MediaScannerNotifier(Activity activity, File file, Intent intent, OnViewErrorListener listener, | |
Object cookie) { | |
mFile = file; | |
mIntent = intent; | |
mListener = listener; | |
mCookie = cookie; | |
mActivity = activity; | |
mHandler = new Handler(this); | |
mConnection = new MediaScannerConnection(activity, this); | |
connect(); | |
} | |
@Override | |
public void onMediaScannerConnected() { | |
MyLog.i(TAG, "Submitting %s for scanning", mFile); | |
mConnection.scanFile(mFile.getAbsolutePath(), null); | |
} | |
@Override | |
public void onScanCompleted(String path, Uri uri) { | |
MyLog.i(TAG, "Scan complete", mFile); | |
if (mIntent != null) { | |
mHandler.sendEmptyMessage(0); | |
} | |
mConnection.disconnect(); | |
} | |
@Override | |
public boolean handleMessage(Message msg) { | |
try { | |
mActivity.startActivity(mIntent); | |
} catch (ActivityNotFoundException x) { | |
if (mListener != null) { | |
mListener.OnViewError(mCookie); | |
} | |
} | |
return true; | |
} | |
private void connect() { | |
MyLog.i(TAG, "Connecting to media scanner service"); | |
mConnection.connect(); | |
} | |
private Context mContext; | |
private Activity mActivity; | |
private File mFile; | |
private MediaScannerConnection mConnection; | |
private Intent mIntent; | |
private OnViewErrorListener mListener; | |
private Object mCookie; | |
private Handler mHandler; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment