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

来源:互联网 发布:围棋记谱软件安卓版 编辑:程序博客网 时间:2024/05/17 09:26

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




2 0
原创粉丝点击