码迷,mamicode.com
首页 > 其他好文 > 详细

文件管理器源码分析(一)

时间:2016-05-07 07:51:52      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

技术分享Aboutmillet file management source code analysis




Openthe file manager millet, we will soon see the interface as shownbelow:

Amongthem, will be a variety of document classification. And show thenumber of each file.

技术分享Androidframework of the MediaStore has provided us with the correspondingfunction. For various types of documents, there is a ContentProviderto provide the appropriate data. We need to go through the correctContentProvider to query the corresponding URI can be.


First,define the different types of files in the form of an enumeration:


publicenumFileCategory{

    All,Music, Video, Picture, Theme, Doc, Zip, Apk, Custom, Other, Favorite

}


Next,set a variety of file query uri:


//query database

StringvolumeName = "external";

 

Uriuri = Audio.Media.getContentUri(volumeName);  //音频文件

refreshMediaCategory(FileCategory.Music,uri);

 

uri= Video.Media.getContentUri(volumeName);  //视频文件

refreshMediaCategory(FileCategory.Video,uri);

 

uri= Images.Media.getContentUri(volumeName);  //图片文件

refreshMediaCategory(FileCategory.Picture,uri);

 

uri= Files.getContentUri(volumeName);   //其他文件

refreshMediaCategory(FileCategory.Theme,uri);

refreshMediaCategory(FileCategory.Doc,uri);

refreshMediaCategory(FileCategory.Zip,uri);

refreshMediaCategory(FileCategory.Apk,uri);



Amongthem, MediaStore.Files contains the audio files, video files, imagefiles, and other types of documents.


Afterthat, we see what the refreshMediaCategory method does:


privatebooleanrefreshMediaCategory(FileCategoryfc, Uri uri) {

    String[]columns = new String[]{

            "COUNT(*)","SUM(_size)"

    };

    Cursorc = mContext.getContentResolver().query(uri, columns,buildSelectionByCategory(fc), null, null);

    if(c == null) {

        Log.e(LOG_TAG,"fail to query uri:" +uri);

        returnfalse;

    }

 

    if(c.moveToNext()) {

        setCategoryInfo(fc,c.getLong(0), c.getLong(1));

        Log.v(LOG_TAG,"Retrieved " +fc.name() + " info >>> count:"+ c.getLong(0) + "size:"+c.getLong(1));

        c.close();

        returntrue;

    }

 

    returnfalse;

}


Thisis carried out on the ContentProvider query, according to theconditions set in the buildSelectionByCategory method to query.

Andlook at the contents of the buildSelectionByCategory method:


privateStringbuildSelectionByCategory(FileCategory cat) {

    Stringselection = null;

    switch(cat) {

        caseTheme:

            selection= FileColumns.DATA + " LIKE ‘%.mtz‘";

            break;

        caseDoc:

            selection= buildDocSelection();

            break;

        caseZip:

            selection= "(" +FileColumns.MIME_TYPE + " == ‘"+Util.sZipFileMimeType + "‘)";

            break;

        caseApk:

            selection= FileColumns.DATA + " LIKE ‘%.apk‘";

            break;

        default:

            selection= null;

    }

    returnselection;

}


Becauseonly the contents of the file in MediaStore.Files need to bedistinguished, so the condition query only needs to be done on this.

Forcompressed files (Zip) is:

privateStringbuildDocSelection() {

    StringBuilderselection = new StringBuilder();

    Iterator<String>iter = Util.sDocMimeTypesSet.iterator();

    while(iter.hasNext()){

        selection.append("("+FileColumns.MIME_TYPE + "==‘"+ iter.next() +"‘) OR ");

    }

    return selection.substring(0,selection.lastIndexOf(")") + 1);

}



Amongthem, sDocMimeTypesSet is:

publicstatic HashSet<String> sDocMimeTypesSet = new HashSet<String>(){

    {

        add("text/plain");

        add("text/plain");

        add("application/pdf");

        add("application/msword");

        add("application/vnd.ms-excel");

        add("application/vnd.ms-excel");

    }

};


Inthis way, we will know how to use the MediaStore file manager toachieve a variety of types of files through the function to achievevery quickly




文件管理器源码分析(一)

标签:

原文地址:http://blog.csdn.net/jjz_bobo/article/details/51332610

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!