天天看點

3-AIV--使用ContentProvider獲得所有圖檔路徑

零、前言

[1].顧名思義,内容提供者,目的:實作跨程序間資料共享

[2].基于資料庫提供資料

[3].如電話簿,短信,歌曲資訊都是以資料庫存儲存儲,都可以通過ContentProvider擷取

[4].如果隻是想使用的人,直接把靜态方法拷貝即可,想知道原理的,圖檔和注釋認真看一下,也可以自己分析一下聯系人的表。

[5].本文隻列印一下查詢結果:RecycleView篇将會界面展示資料,Xml篇将會介紹備份到本地

一、代碼實作

1.實體類
/**
 * 作者:張風捷特烈
 * 時間:2018/4/18:11:36
 * 郵箱:[email protected]
 * 說明:檔案夾實體類
 */
public class FolderBean {

    /**
     * 目前檔案夾路徑
     */
    private String dir;

    /**
     * 目前檔案夾第一個照片的路徑
     */
    private String firstImgPath;
    /**
     *
     */
    private String name;
    /**
     * 目前檔案夾内圖檔數量
     */
    private int count;


    public String getDir() {
        return dir;
    }

    public void setDir(String dir) {
        this.dir = dir;
        String[] names = this.dir.split("/");
        this.name = names[names.length - 1];
    }

    public String getFirstImgPath() {
        return firstImgPath;
    }

    public void setFirstImgPath(String firstImgPath) {
        this.firstImgPath = firstImgPath;
    }

    public String getName() {
        return name;
    }


    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }


    @Override
    public String toString() {
        return "FolderBean{" +
                "dir='" + dir + '\'' +
                ", firstImgPath='" + firstImgPath + '\'' +
                ", name='" + name + '\'' +
                ", count=" + count +
                '}';
    }
           
2.獲得所有圖檔路徑的封裝方法
/**
 * 作者:張風捷特烈
 * 時間:2018/4/14:10:15
 * 郵箱:[email protected]
 * 說明:擷取手機,聯系人工具類
 */
public class PhoneUtils_Picture {
    /**
     * 最大圖檔數量的檔案夾圖檔數
     */
    public static int mMaxCount = 0;

    /**
     * 最大圖檔數量的檔案夾
     */
    public static File mMaxCountDir = null;

    //////////////////////////擷取手機的圖檔///////////////////////////////////

    public static List<FolderBean> getAllImagePath(Context ctx) {
        List<FolderBean> mFolderBeans = new ArrayList<>();

        //[1]查詢獲得遊标:content://media/external/images/media
        Uri mIngUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        ContentResolver resolver = ctx.getContentResolver();
        Cursor cursor = resolver.query(mIngUri, null,
                MediaStore.Images.Media.MIME_TYPE + "=? or "
                        + MediaStore.Images.Media.MIME_TYPE + "=?",
                new String[]{"image/jpeg", "image/png"},
                MediaStore.Images.Media.DATE_MODIFIED);

        //[2]通過遊标擷取path,建立folderBean對象并指派
        //[2-1]為避免重複掃描,将dirPath放入HashSet集合
        Set<String> mDirPaths = new HashSet<>();
        while (cursor.moveToNext()) {
            //擷取資料庫中圖檔路徑:/storage/emulated/0/DCIM/Camera/IMG20160501152640.jpg
            String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
            //擷取父目錄:/storage/emulated/0/DCIM/Camera
            File parentFile = new File(path).getParentFile();
            //沒有父目錄,跳出本次循環
            if (parentFile == null) continue;
            //聲明實體對象
            FolderBean folderBean;
            //父目錄的絕對路徑:/storage/emulated/0/DCIM/Camera
            String dirPath = parentFile.getAbsolutePath();
            if (mDirPaths.contains(dirPath)) {
                continue;//集合中有這個目錄 跳出本次循環
            } else {//集合中沒有這個目錄
                //加入集合
                mDirPaths.add(dirPath);
                //建立實體對象
                folderBean = new FolderBean();
                //父檔案夾設定到folderBean
                folderBean.setDir(dirPath);
                //第一張圖檔路徑設定到folderBean
                folderBean.setFirstImgPath(path);
            }

            if (parentFile.list() != null) {
                //根據父檔案夾,過濾出所有以jpg,png,jpeg結尾的檔案的數量
                int imgCount = parentFile.list(new FilenameFilter() {
                    @Override
                    public boolean accept(File dir, String name) {
                        return name.endsWith(".jpg") || name.endsWith(".png") || name.endsWith(".jpeg");
                    }
                }).length;

                if (mMaxCount <= imgCount) {
                    mMaxCount = imgCount;
                    mMaxCountDir = parentFile;
                }

                //設定檔案夾下圖檔的數量
                folderBean.setCount(imgCount);
                //加入集合
                mFolderBeans.add(folderBean);
            }
        }
        cursor.close();
        return mFolderBeans;
    }
}
           
3.使用:
注意:查詢資料庫是耗時操作,為了不阻塞主線程,最好建立個線程操作
new Thread(new Runnable() {
    @Override
    public void run() {
        List<FolderBean> allImagePath = PhoneUtils_Picture.getAllImagePath(MainActivity.this);
        System.out.println(
        PhoneUtils_Picture.mMaxCountDir+ "檔案夾圖檔數最多,有" +
        PhoneUtils_Picture.mMaxCount+"張");
        for (FolderBean folderBean : allImagePath) {
        System.out.println(folderBean);
        }
    }
}).start();
           
4.結果:

圖檔.png

後記、

1.聲明:

[1]本文由張風捷特烈原創,轉載請注明

[2]歡迎廣大程式設計愛好者共同交流

[3]個人能力有限,如有不正之處歡迎大家批評指證,必定虛心改正

[4]你的喜歡與支援将是我最大的動力

2.連接配接傳送門:
更多安卓技術歡迎通路:安卓技術棧 我的github位址:歡迎star 簡書首發,騰訊雲+社群同步更新 張風捷特烈個人網站,程式設計筆記請通路: http://www.toly1994.com
3.聯系我

QQ:1981462002

郵箱:

[email protected] 微信:zdl1994328
4.歡迎關注我的微信公衆号,最新精彩文章,及時送達:

公衆号.jpg