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

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值