文件管理器源码分析(一)
来源:互联网 发布:围棋记谱软件安卓版 编辑:程序博客网 时间: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:
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 = 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);
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 = 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
- 文件管理器源码分析(一)
- 文件管理器源码分析(二)
- 文件管理器源码分析(三)
- 文件管理器源码分析(四)
- 文件管理器源码分析(五)
- Mangos源码分析(一):DBC文件分析
- ThinkPHP3.1.3源码分析(一) 入口文件分析
- 源码分析(一)
- OpenStack Swift源码分析(一)builder文件的生成
- Alluxio源码分析写数据:创建文件(一)
- Alluxio源码分析读数据:打开文件(一)
- RocketMQ源码分析(一): 索引文件(IndexFile)
- 毕业设计(手机文件管理器源码)
- Scrapy源码分析(三):信号管理器SignalManager
- libsvm源码分析(一):svm.h文件
- HDFS源码分析(一)-----INode文件节点
- HDFS源码分析(一)-----INode文件节点
- Magento源码分析笔录一:入口文件
- 垃圾收集器
- easy-ui之datagrid 单元格样式设置
- java保留4位小数
- Windows 08R2 IIS网站架设
- 解决ViewPager.OnPageChangeListener 和 OnClickListener 冲突的有关问题
- 文件管理器源码分析(一)
- 出轨后身体留下的六个特征
- 第八届中国云计算大会发来贺电 | 有容云将作为【云计算优秀项目】特邀嘉宾出席
- 关于DBN的理解
- Mysql的列索引和多列索引
- 约瑟夫问题
- Android相对布局(RelativeLayout)
- 第六章: types and declarations
- freescale mx6q平台上添加spi资源