Giter Club home page Giter Club logo

contentresolverdemo's Introduction

项目开发中我们要使用到本地SD卡中的媒体文件,ContentResolver 可以很方便的帮助我们查询所有信息。

--

1.ContentResolver 中我们要使用到的两个方式的讲解

通过 mContext.getContentResolver();获取ContentResolver 实例,查询使用query 插入使用insert

query(...) 搜索指定Uri下的媒体文件,后面是sql语句

insert(...) 把新文件插入到指定Uri表中,后面跟数据库键值对。

2.具体参数的介绍

uri:用于检索内容的 URI projection:要返回的列的列表。传递 null 时,将返回所有列,这样会导致效率低下 selection:一种用于声明要返回哪些行的过滤器,其格式为 SQL WHERE 子句(WHERE 本身除外)。传递 null 时,将为指定的 URI 返回所有行 selectionArgs:您可以在 selection 中包含 ?s,它将按照在 selection 中显示的顺序替换为 selectionArgs 中的值。该值将绑定为字串符 sortOrder:行的排序依据,其格式为 SQL ORDER BY 子句(ORDER BY 自身除外)。传递 null 时,将使用默认排序顺序(可能并未排序)

3.媒体文件的Uri是如何获取的

找到MediaStore,里面内部类有Images,Audio,Video,Files这几个包含了所有Android媒体类型,例如我们要查询图片则通过Images 得到对应的EXTERNAL_CONTENT_URI就能按照ContentResolver 的方法查询图片,同时Images 中还有缩略图类,可以通过查询到图片的缩略图,表的字段名都一样,关键也是Uri,可以通过Images中的Thumbnails获取。以此类推可以去看看其他几种媒体类型中的相关Uri以及字段名和能查询到的信息。

3. 具体的方法实现

  1. 查询图片
	private List<FileItem> getAllPhoto() {

		List<FileItem> photos = new ArrayList<>();

		String[] projection = new String[]{MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATA, MediaStore.Images.ImageColumns.DISPLAY_NAME};


		//asc 按升序排列
		//    desc 按降序排列
		//projection 是定义返回的数据,selection 通常的sql 语句,例如  selection=MediaStore.Images.ImageColumns.MIME_TYPE+"=? " 那么 selectionArgs=new String[]{"jpg"};
		Cursor cursor = mContentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Images.ImageColumns.DATE_MODIFIED + "  desc");


		String imageId = null;

    	String fileName;

    	String filePath;

    	while (cursor.moveToNext()) {

      	imageId = cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns._ID));

      	fileName = cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DISPLAY_NAME));

      	filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA));

      	FileItem fileItem = new FileItem(imageId, filePath, fileName);

      	Log.e("ryze_photo", imageId + " -- " + fileName + " -- " + filePath);


      	photos.add(fileItem);


    	}
   	 	cursor.close();

    	cursor = null;

    	return photos;

  	}

2 .查询文本文件

	 private List<FileItem> getAllText() {

    List<FileItem> texts = new ArrayList<>();

    String[] projection = new String[]{MediaStore.Files.FileColumns._ID, MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.TITLE, MediaStore.Files.FileColumns.MIME_TYPE};

    //相当于我们常用sql where 后面的写法
    String selection = MediaStore.Files.FileColumns.MIME_TYPE + "= ? "
        + " or " + MediaStore.Files.FileColumns.MIME_TYPE + " = ? "
        + " or " + MediaStore.Files.FileColumns.MIME_TYPE + " = ? "
        + " or " + MediaStore.Files.FileColumns.MIME_TYPE + " = ? "
        + " or " + MediaStore.Files.FileColumns.MIME_TYPE + " = ? ";

    String[] selectionArgs = new String[]{"text/plain", "application/msword", "application/pdf", "application/vnd.ms-powerpoint", "application/vnd.ms-excel"};

    Cursor cursor = mContentResolver.query(MediaStore.Files.getContentUri("external"), projection, selection, selectionArgs, MediaStore.Files.FileColumns.DATE_MODIFIED + " desc");


    String fileId;

    String fileName;

    String filePath;

    while (cursor.moveToNext()) {

      fileId = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns._ID));

      fileName = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.TITLE));

      filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA));


      Log.e("ryze_text", fileId + " -- " + fileName + " -- " + "--" + cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.MIME_TYPE)) + filePath);

      FileItem fileItem = new FileItem(fileId, filePath, fileName);

      texts.add(fileItem);

    }


    cursor.close();
    cursor = null;


    return texts;

  }

4. 实现效果

分类展示

图片展示

5.源码和apk

demo

APK

contentresolverdemo's People

Contributors

goodbranch avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.