Android中多媒体文件、文档以及各类文件的获取

最近项目中需要对好友进行发送文件需求,在android中对文件以及其它文档的获取一直没有找到一个好的案例,自己在分析“仿小米文件夹管理”时感觉自己非常的菜B,看不懂源代码。我也问了他人的思路,但是他们给我递归方法去扫描感觉很2B,想想在android中就QQ的文件目录都大得吓尿,所以还是安安静静的用内容提供者吧。看到有很多的同行也在找想关的资料,希望我的分享对大家有帮助,谢谢!

      在android中其实系统已经给我们提供了一个数据库,里面包含了sd卡中所有文件的信息(大小、位置、创建时间等),所以我们只需要得到内容提供者的URI以及参数列表就O了,多媒体数据库存放的位置data/data/com.android.provider.media/databases/external.db与data/data/com.android.provider.media/databases/internal.db中,目录无法打开的同学可以安装一个模拟器进行查看。下面是代码:

       注意:因为这两个数据库里面的数据是非常的多,所以建议大家开子线程获取数据。子线程获取后推送到主线程这个就不用说了吧。 

       首先是搜索文档的:      

       下面是获取视频文件的

public List<Document> getDocumentListData() {
		
		String[] columns = new String[] {MediaStore.Files.FileColumns._ID,MediaStore.Files.FileColumns.MIME_TYPE,MediaStore.Files.FileColumns.SIZE,MediaStore.Files.FileColumns.DATE_MODIFIED,MediaStore.Files.FileColumns.DATA };
		
		String select = "(" + FileColumns.DATA + " LIKE '%.doc'" + " or " + FileColumns.DATA + " LIKE '%.docx'" + " or " + FileColumns.DATA + " LIKE '%.xls'" + " or " + FileColumns.DATA + " LIKE '%.ppt'" + " or " + FileColumns.DATA + " LIKE '%.txt'" + ")";
		
		List<Document> dataList = new ArrayList<Document>();
		ContentResolver contentResolver = mContext.getContentResolver();
		Cursor cursor = contentResolver.query(MediaStore.Files.getContentUri("external"), columns, select,null, null);
		 
		int columnIndexOrThrow_ID = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns._ID);
		int columnIndexOrThrow_MIME_TYPE = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.MIME_TYPE);
		int columnIndexOrThrow_DATA = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA);
		int columnIndexOrThrow_SIZE = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.SIZE);
		int columnIndexOrThrow_DATE_MODIFIED = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATE_MODIFIED); // 创建时间,更改时间
		
        if (cursor != null) {
            while (cursor.moveToNext()) {
            	
            	String path = cursor.getString(columnIndexOrThrow_DATA);
            	int position_do = path.lastIndexOf(".");
            	if( position_do == -1 ) continue;
            	String type = path.substring( position_do+1, path.length() );
            	int doc_type = 0;
            	if( type.contains("doc") ) {
            		doc_type = FileTypeConfig.DOC;
            	} else if( type.contains("xls") ) {
            		doc_type = FileTypeConfig.XLS;
            	} else if( type.contains("ppt") ) {
            		doc_type = FileTypeConfig.PPT;
            	} else if( type.contains("txt") ) {
            		doc_type = FileTypeConfig.TXT;
            	} else {
            		continue;
            	}
            	int position_x = path.lastIndexOf( File.separator );
            	if( position_x == -1 ) continue;
            	String displayName = path.substring( position_x + 1 , path.length() );
                int id = cursor.getInt( columnIndexOrThrow_ID );
                String mimeType = cursor.getString(columnIndexOrThrow_MIME_TYPE);
                long size = cursor.getLong(columnIndexOrThrow_SIZE);
                long modified_date = cursor.getLong(columnIndexOrThrow_DATE_MODIFIED);
                
                Document document = new Document( id ,displayName,mimeType,path, size,modified_date, doc_type );
                document.setSelected( false );
                dataList.add(document);
            }
        }
        return dataList;
	}
下面是获取视频文件的

	/**
	 * 获取视频文件
	 * @return
	 */
	public List<Video> getVedioListData() {
		
		List<Video> dataList = new ArrayList<Video>();
		ContentResolver contentResolver = mContext.getContentResolver();
		String[] columns = new String[] { MediaStore.Video.Media._ID,
				MediaStore.Video.Media.MIME_TYPE, MediaStore.Video.Media.ALBUM,
				MediaStore.Video.Media.ARTIST,
				MediaStore.Video.Media.DISPLAY_NAME,
				MediaStore.Video.Media.DATA, MediaStore.Video.Media.DURATION,
				MediaStore.Video.Media.SIZE,
				MediaStore.Video.Media.DATE_MODIFIED };
		Cursor cursor = contentResolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, columns, null,null, null);
		 
		int columnIndexOrThrow_ID = cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID);
		int columnIndexOrThrow_ALBUM = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.ALBUM);
		int columnIndexOrThrow_ARTIST = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.ARTIST);
		int columnIndexOrThrow_DISPLAY_NAME = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
		int columnIndexOrThrow_MIME_TYPE = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE);
		int columnIndexOrThrow_DATA = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
		int columnIndexOrThrow_DURATION = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION);
		int columnIndexOrThrow_SIZE = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE);
		int columnIndexOrThrow_DATE_MODIFIED = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATE_MODIFIED); // 创建时间,更改时间
		
        BitmapFactory.Options options = new BitmapFactory.Options();  
        options.inDither = false;  
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
		 
        if (cursor != null) {
            while (cursor.moveToNext()) {
                int id = cursor.getInt( columnIndexOrThrow_ID );
                String album = cursor.getString(columnIndexOrThrow_ALBUM);
                String artist = cursor.getString(columnIndexOrThrow_ARTIST);
                String displayName = cursor.getString(columnIndexOrThrow_DISPLAY_NAME);
                String mimeType = cursor.getString(columnIndexOrThrow_MIME_TYPE);
                String path = cursor.getString(columnIndexOrThrow_DATA);
                long duration = cursor.getInt(columnIndexOrThrow_DURATION);
                long size = cursor.getLong(columnIndexOrThrow_SIZE);
                long modified_date = cursor.getLong(columnIndexOrThrow_DATE_MODIFIED);
                // icon 视频缩略图
                Bitmap bitmap = MediaStore.Video.Thumbnails.getThumbnail(contentResolver,id,Images.Thumbnails.MICRO_KIND, options); 
                Video video = new Video(id, displayName, album, artist, displayName, mimeType, path, size, duration,modified_date,bitmap, FileTypeConfig.VIO);
                dataList.add(video);
            }
        }
        return dataList;
	}

音乐文件的获取URI:MediaStore.Audio.Media.getContentUri("external");

如果各位有更好的方法或者说更改的优化,欢迎sao扰。

如果感兴趣的可以加QQ群:486302449

版权声明:本文为博主原创文章,转载请注明出处。

发布了1 篇原创文章 · 获赞 3 · 访问量 4200
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览