Android开发之多媒体文件获取工具类实例【音频,视频,图片等】
本文实例讲述了Android开发之多媒体文件获取工具类。分享给大家供大家参考,具体如下:
packagecom.android.ocr.util; importjava.io.File; importjava.util.ArrayList; importjava.util.List; importandroid.content.Context; importandroid.database.Cursor; importandroid.graphics.Bitmap; importandroid.provider.MediaStore; /** *根据MediaStore查询信息 *@ProjectApp_ReadCard *@Packagecom.android.ocr.util *@authorchenlin *@version1.0 *@Date2013年6月16日 *@NoteTODO */ publicclassMediaStoreUtil{ privatestaticfinalStringTAG="MediaStoreUtil"; /** *查询音频文件名称 * *@paramcontext *@return */ publicstaticListgetAudioNames(Contextcontext){ List list=newArrayList (); Cursorcursor=context.getContentResolver().query( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, newString[]{MediaStore.Audio.Media._ID,MediaStore.Audio.Media.DISPLAY_NAME,MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DURATION,MediaStore.Audio.Media.ARTIST,MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.YEAR,MediaStore.Audio.Media.MIME_TYPE,MediaStore.Audio.Media.SIZE, MediaStore.Audio.Media.DATA},null,newString[]{},null); while(cursor.moveToNext()){ StringfileName=cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)); list.add(fileName); } returnlist; } /** *查询图片文件名称 * *@paramcontext *@return */ publicstaticList getImageNames(Contextcontext){ List list=newArrayList (); Cursorcursor=context.getContentResolver().query( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, newString[]{MediaStore.Images.Media._ID,MediaStore.Images.Media.DISPLAY_NAME,MediaStore.Images.Media.TITLE, MediaStore.Images.Media.MIME_TYPE,MediaStore.Images.Media.SIZE,MediaStore.Images.Media.DATA},null, newString[]{},null); while(cursor.moveToNext()){ Logger.i(TAG,"filePath=="+MediaStore.Images.Media.DATA); StringfilePath=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)); StringfileName=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME)); list.add(filePath+"/"+fileName); } returnlist; } /** *查询图片文件 * *@paramcontext *@return */ publicstaticList getImages(Contextcontext){ List list=newArrayList (); Cursorcursor=context.getContentResolver().query( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, newString[]{MediaStore.Images.Media._ID,MediaStore.Images.Media.DISPLAY_NAME,MediaStore.Images.Media.TITLE, MediaStore.Images.Media.MIME_TYPE,MediaStore.Images.Media.SIZE,MediaStore.Images.Media.DATA},null, newString[]{},null); while(cursor.moveToNext()){ StringfilePath=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)); Logger.i(TAG,"filePath=="+filePath); StringfileName=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME)); //Logger.i(TAG,"fileName=="+fileName); Filefile=newFile(filePath); list.add(file); } returnlist; } /** *查询文件 * *@paramcontext *@return */ publicstaticList getAllFiles(Contextcontext){ List list=newArrayList (); Cursorcursor=context.getContentResolver().query( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, newString[]{MediaStore.Images.Media._ID,MediaStore.Images.Media.DISPLAY_NAME,MediaStore.Images.Media.TITLE, MediaStore.Images.Media.MIME_TYPE,MediaStore.Images.Media.SIZE,MediaStore.Images.Media.DATA},null, newString[]{},null); while(cursor.moveToNext()){ StringfilePath=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)); Logger.i(TAG,"filePath=="+filePath); StringfileName=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME)); //Logger.i(TAG,"fileName=="+fileName); Filefile=newFile(filePath); list.add(file); } returnlist; } /** *获取所有的缩列图 * *@paramcontext *@return */ publicstaticBitmap[]getBitmaps(Contextcontext){ Bitmap[]bitmaps; String[]projection={MediaStore.Images.Media._ID,MediaStore.Images.Media.DATA}; Cursorcursor=context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,projection,null,null, MediaStore.Images.Media._ID); intcount=cursor.getCount(); intimage_column_index=cursor.getColumnIndex(MediaStore.Images.Media._ID); bitmaps=newBitmap[count]; for(inti=0;i getThumbNames(Contextcontext){ List list=newArrayList (); Cursorcursor=context.getContentResolver().query( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, newString[]{MediaStore.Images.Thumbnails._ID,MediaStore.Images.Thumbnails.DATA,MediaStore.Images.Thumbnails.KIND, MediaStore.Images.Thumbnails.IMAGE_ID},null,newString[]{},null); while(cursor.moveToNext()){ StringfileName=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME)); list.add(fileName); } returnlist; } /** *获得所有视频文件 *@paramcontext */ publicstaticArrayList getVideoInfo(Contextcontext){ String[]thumbColumns=newString[]{ MediaStore.Video.Thumbnails.DATA, MediaStore.Video.Thumbnails.VIDEO_ID }; String[]mediaColumns=newString[]{ MediaStore.Video.Media.DATA, MediaStore.Video.Media._ID, MediaStore.Video.Media.TITLE, MediaStore.Video.Media.MIME_TYPE }; //首先检索SDcard上所有的video Cursorcursor=context.getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,mediaColumns,null,null,null); ArrayList videoList=newArrayList (); if(cursor.moveToFirst()){ do{ VideoInfoinfo=newVideoInfo(); info.filePath=cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA)); info.mimeType=cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE)); info.title=cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.TITLE)); //获取当前Video对应的Id,然后根据该ID获取其Thumb intid=cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID)); Stringselection=MediaStore.Video.Thumbnails.VIDEO_ID+"=?"; String[]selectionArgs=newString[]{ id+"" }; CursorthumbCursor=context.getContentResolver().query(MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,thumbColumns,selection,selectionArgs,null); if(thumbCursor.moveToFirst()){ info.thumbPath=cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Thumbnails.DATA)); } //然后将其加入到videoList videoList.add(info); }while(cursor.moveToNext()); } returnvideoList; } staticclassVideoInfo{ StringfilePath; StringmimeType; StringthumbPath; Stringtitle; } }
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android多媒体操作技巧汇总(音频,视频,录音等)》、《Android开发入门与进阶教程》、《Android视图View技巧总结》、《Android文件操作技巧汇总》、《Android资源操作技巧汇总》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。