android多媒体数据库添加音视频数据项
2014-12-10 10:50
239 查看
上次提到手机全盘扫描歌曲or视频文件的例子,后来应用发现当app所用的是系统对媒体数据库的时候就不怎么适用了,现在写一个往系统多媒体数据库中添加数据项的例子。因为SDK 4.4以上就不能向系统发送广播更新 多媒体数据库,且 MediaScannerConnection.scanFile 方法所耗时间也太长,且是一个异步操作,等待其返回又是一个麻烦的操作。所以我的思路是:还是像上次一样 全盘扫描 音频文件,当发现有的时候,再判断 数据库中是否有,没有的话就添加进去。当然我的判断有没有是直接根据名字判断的,想要精确点的话,可以用路径去判断,好了,不说废话了,直接贴代码:
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; } }
相关文章推荐
- Android 广告(banner)图片轮播、图片浏览、仿微信大图查看控件(支持视频和gif图片)、支持动态添加数据
- android 数据库 SQLiteOpenHelper和ContentProvider学习笔记---添加数据及显示(一)
- Android中给Spinner或ListView添加数据
- 【Android游戏开发之六】在SurfaceView中添加组件!!!!并且相互交互数据!!!!
- 在Android 中调用选择图片、视频、添加音频、录音、拍摄视频、拍照等其他的功能
- Android中抓取手机视频流数据。
- Android中给Spinner或ListView添加数据
- android 为 ListView Item中的组件添加事件 以及更新数据
- Android 如何用HttpClient 以Get方式获取数据并添加http头信息
- Android 如何用HttpClient 以Post方式提交数据并添加http头信息
- android ListView内数据的动态添加与删除
- 【Android游戏开发之六】在SurfaceView中添加组件!!!!并且相互交互数据!!!!
- Android为Spinner添加数据
- Android 之往通讯录中添加数据和查询数据
- Android深入探究笔记之十 -- 使用 ContentProvider 共享数据(二),访问与添加通讯录
- android 数据库 SQLiteOpenHelper和ContentProvider学习笔记---添加修改删除数据之联系人(二)
- Android系统中视频电话中camera部分数据流分析
- android 为 ListView Item中的组件添加事件 以及更新数据
- Android 如何使用juv-rtmp-client.jar向Red5服务器发布实时视频数据
- 向Android系统自带的联系人界面添加数据