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:
public
enum
FileCategory{
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:
private
boolean
refreshMediaCategory(FileCategoryfc,
Uri uri) {
String[]columns
= newString[]{
"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);
return
false;
}
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();
return
true;
}
return
false;
}
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:
private
StringbuildSelectionByCategory(FileCategory
cat) {
Stringselection
= null;
switch
(cat)
{
case
Theme:
selection=
FileColumns.DATA + " LIKE ‘%.mtz‘";
break;
case
Doc:
selection=
buildDocSelection();
break;
case
Zip:
selection=
"("+FileColumns.MIME_TYPE + " == ‘"
+Util.sZipFileMimeType
+ "‘)";
break;
case
Apk:
selection=
FileColumns.DATA + " LIKE ‘%.apk‘";
break;
default:
selection=
null;
}
return
selection;
}
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:
private
StringbuildDocSelection()
{
StringBuilderselection
= newStringBuilder();
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