1.概述

在Andorid系统中所有的文件路径都保存在一个数据库中,位于data/data/com.android.providers.media文件夹下的external.db

里面的files表就有我们需要的内容,这个表包含了机器所有的文件。接下来只要选择合适的sql语句来获取我们需要的内容就行了。

2.实现

数据库结构如下图

首先过滤出相册文件夹,获取所在文件夹路径,文件夹名称,文件夹文件数目,文件路径和修改时间。构建文件夹和封面

public static Cursor getAlbumCursor(ContentResolver cr){
//打开files这个表Uri uri= MediaStore.Files.getContentUri("external");String [] project =new String[]{MediaStore.Files.FileColumns.PARENT,MediaStore.Images.Media.BUCKET_DISPLAY_NAME,"count(*)",MediaStore.Images.Media.DATA,"max(" + MediaStore.Images.Media.DATE_MODIFIED + ")"};
//用group by进行整合String selection = String.format("%s=? or %s=?) group by (%s",MediaStore.Files.FileColumns.MEDIA_TYPE,MediaStore.Files.FileColumns.MEDIA_TYPE,MediaStore.Files.FileColumns.PARENT) ;String[] selectionArgs = new String[]{MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE+"",//获取图片文件MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO+"",//获取视频文件};String sortOrder = MediaStore.Images.Media.DATE_MODIFIED + " desc";
//        String sortOrder = "max(date_modified) DESC";
// 转换成sql语句: SELECT parent, bucket_display_name, count(*), _data, max(date_modified) FROM files WHERE (media_type=? or media_type=?)
// group by (parent) HAVING (_data NOT LIKE ? ) ORDER BY max(date_modified) DESCreturn cr.query(uri,project,selection,selectionArgs,sortOrder);}

拿到cursor后就可以进行查询获取数据

 public Album(Cursor cur) {
//图片路径this(cur.getString(3),
//所在文件夹的名称cur.getString(1),
//所在的父文件cur.getLong(0),
//文件夹的文件数目cur.getInt(2),
//修改时间cur.getLong(4));}

相册文件夹构建好了后,在来获取文件夹里面的所有的图片

 public static Cursor getMediaCursor(ContentResolver cr,Album album){Uri uri = MediaStore.Files.getContentUri("external");String[] sProjection = new String[] {MediaStore.Images.Media.DATA,MediaStore.Images.Media.DATE_TAKEN,MediaStore.Images.Media.MIME_TYPE,MediaStore.Images.Media.SIZE,MediaStore.Images.Media.ORIENTATION};
//获取parent一样的文件String selections = (String.format("(%s=? or %s=?) and %s=?",MediaStore.Files.FileColumns.MEDIA_TYPE,MediaStore.Files.FileColumns.MEDIA_TYPE,MediaStore.Files.FileColumns.PARENT));String[] selectionArgs = new String[]{MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE+"",MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO+"",album.getId()+""};String sortOrder = MediaStore.Images.Media.DATE_MODIFIED + " desc";return  cr.query(uri,sProjection,selections,selectionArgs,sortOrder);}

接下来构建实体类

 public Media(Cursor cur) {
//具体路径this(cur.getString(0),cur.getLong(1),cur.getString(2),cur.getLong(3),cur.getInt(4));}

这种方式主要是利用好了sql语句,同时结合rxjava的话可以实现查询一张照片显示一张照片的效果。

3.扩展

类似的还可以获取特定类型的文件

private ArrayList<LayoutElementParcelable> listImages() {ArrayList<LayoutElementParcelable> images = new ArrayList<>();final String[] projection = {MediaStore.Images.Media.DATA};final Cursor cursor = c.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,projection, null, null, null);if (cursor == null)return images;else if (cursor.getCount() > 0 && cursor.moveToFirst()) {do {String path = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA));HybridFileParcelable strings = RootHelper.generateBaseFile(new File(path), showHiddenFiles);if (strings != null) {LayoutElementParcelable parcelable = createListParcelables(strings);if(parcelable != null) images.add(parcelable);}} while (cursor.moveToNext());}cursor.close();return images;}private ArrayList<LayoutElementParcelable> listVideos() {ArrayList<LayoutElementParcelable> videos = new ArrayList<>();final String[] projection = {MediaStore.Images.Media.DATA};final Cursor cursor = c.getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,projection, null, null, null);if (cursor == null)return videos;else if (cursor.getCount() > 0 && cursor.moveToFirst()) {do {String path = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA));HybridFileParcelable strings = RootHelper.generateBaseFile(new File(path), showHiddenFiles);if (strings != null) {LayoutElementParcelable parcelable = createListParcelables(strings);if(parcelable != null) videos.add(parcelable);}} while (cursor.moveToNext());}cursor.close();return videos;}private ArrayList<LayoutElementParcelable> listaudio() {String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";String[] projection = {MediaStore.Audio.Media.DATA};Cursor cursor = c.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,projection,selection,null,null);ArrayList<LayoutElementParcelable> songs = new ArrayList<>();if (cursor == null)return songs;else if (cursor.getCount() > 0 && cursor.moveToFirst()) {do {String path = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA));HybridFileParcelable strings = RootHelper.generateBaseFile(new File(path), showHiddenFiles);if (strings != null) {LayoutElementParcelable parcelable = createListParcelables(strings);if(parcelable != null) songs.add(parcelable);}} while (cursor.moveToNext());}cursor.close();return songs;}private ArrayList<LayoutElementParcelable> listDocs() {ArrayList<LayoutElementParcelable> docs = new ArrayList<>();final String[] projection = {MediaStore.Files.FileColumns.DATA};Cursor cursor = c.getContentResolver().query(MediaStore.Files.getContentUri("external"),projection, null, null, null);String[] types = new String[]{".pdf", ".xml", ".html", ".asm", ".text/x-asm", ".def", ".in", ".rc",".list", ".log", ".pl", ".prop", ".properties", ".rc",".doc", ".docx", ".msg", ".odt", ".pages", ".rtf", ".txt", ".wpd", ".wps"};if (cursor == null)return docs;else if (cursor.getCount() > 0 && cursor.moveToFirst()) {do {String path = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA));if (path != null && Arrays.asList(types).contains(path)) {HybridFileParcelable strings = RootHelper.generateBaseFile(new File(path), showHiddenFiles);if (strings != null) {LayoutElementParcelable parcelable = createListParcelables(strings);if(parcelable != null) docs.add(parcelable);}}} while (cursor.moveToNext());}cursor.close();Collections.sort(docs, (lhs, rhs) -> -1 * Long.valueOf(lhs.date).compareTo(rhs.date));if (docs.size() > 20)for (int i = docs.size() - 1; i > 20; i--) {docs.remove(i);}return docs;}private ArrayList<LayoutElementParcelable> listApks() {ArrayList<LayoutElementParcelable> apks = new ArrayList<>();final String[] projection = {MediaStore.Files.FileColumns.DATA};Cursor cursor = c.getContentResolver().query(MediaStore.Files.getContentUri("external"), projection, null, null, null);if (cursor == null)return apks;else if (cursor.getCount() > 0 && cursor.moveToFirst()) {do {String path = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA));if (path != null && path.endsWith(".apk")) {HybridFileParcelable strings = RootHelper.generateBaseFile(new File(path), showHiddenFiles);if (strings != null) {LayoutElementParcelable parcelable = createListParcelables(strings);if(parcelable != null) apks.add(parcelable);}}} while (cursor.moveToNext());}cursor.close();return apks;}

源码

Android获取手机图片相关推荐

  1. 【小功能2】android获取手机信息(号码,内存,CPU,分辨率,MAC,IP,SD卡,IMEI,经纬度,信号强度等等)

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://2402766.blog.51cto.com/2392766/1080837 为了 ...

  2. Qt for Android获取手机序列号/手机型号/手机制造商

    前言 Qt for Android 获取手机型号/手机制造商/手机序列号,这些是要通过 Android 原生接口才能获取到的, 那么在 Qt 项目中通过 jni 接口调用 Android 原生接口来获 ...

  3. android 获取手机运行的进程

    今天,简单讲讲如何获取系统运行的进程. ActivityManager.RunningAppProcessInfo类与获取正在运行的应用程序 每一个应用程序都会运行在它独立的进程里,但是为了节省资源或 ...

  4. android 获取已安装 错误代码,android获取手机已经安装的app信息

    Android获取手机已安装APP(系统/非系统) 效果图 主体代码 private ListView mlistview; private ListpackageInfoList; private ...

  5. 获取android型号代码,Android应用开发之Android获取手机品牌、手机型号、手机唯一序列号的代码教程...

    本文将带你了解Android应用开发Android获取手机品牌.手机型号.手机唯一序列号的代码教程,希望本文对大家学Android有所帮助. Android获取手机品牌.手机型号.手机唯一序列号的代码 ...

  6. Android获取手机序列号

    Android获取手机序列号 String androidId = null; androidId = Secure.getString(getContentResolver(), Secure.AN ...

  7. Android获取手机的卡槽数量和sim卡数量

    Android获取手机的卡槽数量和sim卡数量 获取手机卡槽数量,即手机是单卡槽还是双卡槽的方法: TelephonyManager tm = (TelephonyManager) getSystem ...

  8. android获取手机sim卡信息,Android获取手机SIM卡运营商信息的方法

    本文实例讲述了Android获取手机SIM卡运营商信息的方法,对于Android程序设计有非常实用的价值.分享给大家供大家参考之用.具体方法如下: 主要功能代码如下: /** * 获取SIM卡运营商 ...

  9. android 通过手机号码查询联系人,android获取手机通讯录联系人

    android获取手机通讯录联系人信息 private void getPhoneContacts() { ContentResolver resolver = this.getContentReso ...

最新文章

  1. 现行技术体系的问题总结
  2. yolov5和yolov5-face nms比较
  3. SSH(Struts2+Hibernate+Spring)开发策略
  4. 数据聚合Spring Data MongoDB:嵌套结果
  5. oracle导出审计表,Oracle审计表AUD$处理方法
  6. 【python零基础入门学习】Python入门,带你快速学习Python 基础语法
  7. python】字符串练习题
  8. 【嵌入式】第一次大作业_记录环境温度并存入数据库
  9. mysql使用Navicat 导出和导入数据库
  10. 小萝贝控机大师工具推荐(一款在PC就能控制手机界面的工具)
  11. 时空数据生成对抗网络研究综述(下)
  12. 计算机高校教师面试试讲和答辩,高校教师试讲答辩面试考试流程
  13. 自动驾驶之-MATLAB环境下基于深度学习的语义分割
  14. 高三学习计划作文计算机专业,高三学习计划作文.docx
  15. C语言 日期转时间戳
  16. LV算法和回溯法结合解n后问题
  17. stm32正常运行流程图_STM32单片机学习笔记(超详细整理143个问题,学习必看)...
  18. Java的语言基础(一)
  19. MATLAB常用命令及函数大全(字母顺序)
  20. 2023年科普新书大盘点:重磅新书,值得期待!

热门文章

  1. Sublime Text常用快捷键及插件配置
  2. 一个F范数对矩阵求导例子
  3. Bitlocker恢复密钥验证方法
  4. MFP and MOP
  5. ios点击推送闪退_科普:苹果手机闪退怎么办?
  6. 2021年危险化学品经营单位安全管理人员报名考试及危险化学品经营单位安全管理人员作业模拟考试
  7. STM32程序编写总流程及技巧
  8. 3518. 进化序列(evolve)
  9. php 微信小程序 循环 多选,微信小程序 for 循环详解
  10. Spark+Hadoop+中台实战pdf -阿里巴巴资深架构师熬几个通宵肛出来的