- import java.io.File;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.ProgressDialog;
- import android.content.ContentResolver;
- import android.content.ContentValues;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.DialogInterface.OnClickListener;
- import android.database.Cursor;
- import android.media.MediaScannerConnection;
- import android.net.Uri;
- import android.os.Environment;
- import android.os.Handler;
- import android.os.Message;
- import android.provider.MediaStore;
- import android.widget.Toast;
- public class MusicFileScanClient {
- private ProgressDialog scanDialog;
- private boolean songs = true;
- private Context whac;
- private int beforecount = 0;
- private Handler handler = null;
- int sd;
- private boolean iscancel = false;
- private boolean b = false;
- private static MusicFileScanClient instance;
- private ContentResolver resolver;
- public static MusicFileScanClient getInstance() {
- if (null == instance) {
- instance = new MusicFileScanClient();
- }
- return instance;
- }
- public MusicFileScanClient() {
- }
- public void init(Context ctx, String path) {
- // client = new MusicSannerClient();
- this.whac = ctx;
- scanDialog = new ProgressDialog(ctx);
- scanDialog.setIndeterminate(true);
- // FIXME 文字替换为资源
- scanDialog.setMessage(ctx.getString(R.string.scanning));
- scanDialog.setButton(ctx.getString(R.string.cancel), new OnClickListener() {@Override public void onClick(DialogInterface arg0, int arg1) {
- // TODO Auto-generated method stub
- iscancel = true;
- }
- });
- //不能取消
- scanDialog.setCancelable(false);
- String mPath = "/mnt";
- Log.d("MusicFileScanClient", "mPath=" + mPath);
- handler = new Handler() {
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case 1:
- sd = afterScan() - beforecount;
- if (scanDialog != null) {
- scanDialog.dismiss();;
- }
- showWarnDialog(whac.getString(R.string.scan_result) + sd + whac.getString(R.string.scan_result1));
- break;
- case 2:
- if (scanDialog != null) {
- scanDialog.dismiss();;
- }
- showWarnDialog(whac.getString(R.string.scan_result0) + sd + whac.getString(R.string.scan_result1));
- break;
- case 3:
- if (scanDialog != null) {
- scanDialog.dismiss();;
- }
- break;
- }
- }
- };
- }
- private void Scan(File filePath1) {
- if (filePath1 != null && !iscancel) {
- if (filePath1.isDirectory() && !iscancel) {
- File[] files = filePath1.listFiles();
- if (files != null) {
- for (int i = 0; i < files.length; i++) {
- if (!iscancel) {
- if (files[i].isDirectory()) Scan(files[i]);
- else {
- if (songs) {
- for (int k = 0; k < sFilter.length && !iscancel; k++) {
- if (files[i].getName().toLowerCase().endsWith(sFilter[k])) {
- if (cursor != null) {
- cursor.moveToFirst();
- for (int j = 0; j < cursor.getCount(); j++) {
- if (cursor.getString(0).equals(files[i].getName())) {
- break;
- }
- if (!cursor.moveToNext()) {
- String mimeType = "audio/*";
- ContentValues values = new ContentValues();
- values.put(MediaStore.MediaColumns.DATA, files[i].getAbsolutePath());
- values.put(MediaStore.MediaColumns.TITLE, files[i].getName());
- values.put(MediaStore.MediaColumns.MIME_TYPE, mimeType);
- values.put(MediaStore.MediaColumns.SIZE, files[i].length());
- values.put(MediaStore.Audio.Media.DISPLAY_NAME, files[i].getName());
- values.put(MediaStore.Audio.Media.DATA, files[i].getAbsolutePath());
- values.put(MediaStore.Audio.Media.TITLE, files[i].getName());
- values.put(MediaStore.Audio.Media.IS_MUSIC, true);
- Uri uri = MediaStore.Audio.Media.getContentUriForPath(files[i].getAbsolutePath());
- whac.getContentResolver().delete(uri, null, null);
- final Uri newUri = whac.getContentResolver().insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, values);
- //whac.getContentResolver().update(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,values, null, null);
- //updateGallery(files[i].getAbsolutePath());
- Log.d("scan music file = ", "----------->newUri = " + newUri);
- Log.d("scan music file = ", "scan music file = " + files[i].getName());
- }
- }
- }
- }
- }
- } else {
- for (int k = 0; k < sFiltervideo.length && !iscancel; k++) {
- if (files[i].getName().toLowerCase().endsWith(sFiltervideo[k])) {
- if (cursor != null) {
- cursor.moveToFirst();
- for (int j = 0; j < cursor.getCount(); j++) {
- if (cursor.getString(0).equals(files[i].getName())) {
- break;
- }
- if (!cursor.moveToNext()) {
- ContentValues values = new ContentValues();
- values.put(MediaStore.Video.Media.TITLE, files[i].getName());
- values.put(MediaStore.Video.Media.DATA, files[i].getAbsolutePath());
- values.put(MediaStore.Video.Media.DISPLAY_NAME, files[i].getName());
- whac.getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
- //resolver.update(MediaStore.Video.Playlists.EXTERNAL_CONTENT_URI,values, null, null);
- //updateGallery(files[i].getAbsolutePath());
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- private final String[] sFilter = {
- ".aac",
- ".ogg",
- ".mp3",
- ".flac",
- ".alac",
- ".ape",
- ".wma",
- ".wav",
- ".au",
- ".aif",
- ".aiff",
- ".m3u",
- ".m4a",
- ".asf",
- ".mms"
- };
- private final String[] sFiltervideo = {
- ".avi",
- ".3gp",
- ".rmvb",
- ".mp4",
- ".flv",
- };
- public void showWarnDialog(String str) {
- new AlertDialog.Builder(whac).setMessage(str).setPositiveButton(R.string.sure, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialoginterface, int i) {
- dialoginterface.dismiss(); //按钮事件
- }
- }).show();
- }
- public void scanfile(final File f, boolean isSongs) {
- scanDialog.show();
- resolver = whac.getContentResolver();
- songs = isSongs;
- b = false;
- iscancel = false;
- beforecount = beforeScan();
- new Thread(new Runnable() {
- public void run() {
- Scan(f);
- if (!iscancel) {
- handler.sendEmptyMessage(1);
- if (cursor != null) {
- cursor.close();
- cursor = null;
- }
- }
- }
- }).start();
- }
- Cursor cursor = null;
- private int beforeScan() {
- int count = 0;
- if (songs) {
- cursor = MusicUtils.query(whac, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[] {
- MediaStore.Audio.Media.DISPLAY_NAME
- },
- MediaStore.Audio.Media.IS_MUSIC + "=1", null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
- } else {
- cursor = MusicUtils.query(whac, MediaStore.Video.Media.EXTERNAL_CONTENT_URI, new String[] {
- MediaStore.Video.Media.DISPLAY_NAME
- },
- MediaStore.Video.Media.TITLE + " != ''", null, MediaStore.Video.Media.TITLE + " COLLATE UNICODE");
- }
- count = cursor.getCount();
- //cursor.close();
- return count;
- }
- private int afterScan() {
- int count = 0;
- Cursor cursor1 = null;
- if (songs) {
- cursor1 = MusicUtils.query(whac, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[] {
- MediaStore.Audio.Media.DISPLAY_NAME
- },
- MediaStore.Audio.Media.IS_MUSIC + "=1", null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
- } else {
- cursor1 = MusicUtils.query(whac, MediaStore.Video.Media.EXTERNAL_CONTENT_URI, new String[] {
- MediaStore.Video.Media.DISPLAY_NAME
- },
- MediaStore.Video.Media.TITLE + " != ''", null, MediaStore.Video.Media.TITLE + " COLLATE UNICODE");
- }
- if (cursor1 != null) {
- count = cursor1.getCount();
- cursor1.close();
- }
- return count;
- }
- }
来源: http://lib.csdn.net/snippet/android/42745