zoukankan      html  css  js  c++  java
  • MediaStore的应用

    引用:http://www.189works.com/article-30263-1.html

    相信每个使用Android系统的人都会知道Android系统中带有一个图库应用程序或者一个音乐播放器吧。打开图库可以查看到当前终端里所有的图片文件,而音乐播放器可以看到当前终端里所有的MP3文件,而这个打开的过程并不会消耗太多的时间。如果是在打开的时候去扫描所有内存,所有SD卡的话,相信相应是不会这么迅速的。

      后来通过观察终端的Log,发现每次开机时,会有几条tag为MediaScanner的log信息,顾名思义,这是在扫描媒体库,会不会是这个后台服务实现了图库和音乐的快速相应呢?带着此问题去查阅API,果然发现一个强大的类——MediaStore,通过类名很容易能想到,这个类是用于存放多媒体的。此类包含三个内部类,分别为:

      MediaStore.Audio: 存放音频信息

      MediaStore.Image: 存放图片信息

      MediaStore.Vedio: 存放视频信息

      上诉三个内部类又有其各自的内部类,鉴于其结构比较复杂,就不详细去描述了,有兴趣的朋友可以结合API自行研究。

      这三个内部类存储了多媒体的一些基本信息,并通过ContentProvider的数据共享的机制,将其共享出来,提供给各个应用程序使用。下面的例子展示了一个读取图片信息的示例: 

     1 publicclass MainActivity extends Activity {
    2 private ImageView image;
    3 private Button btn;
    4 privateint index;
    5 privateint totalCount;
    6 private ArrayList<String> imageSrcs =new ArrayList<String>();
    7 @Override
    8 publicvoid onCreate(Bundle savedInstanceState) {
    9 super.onCreate(savedInstanceState);
    10 setContentView(R.layout.main);
    11
    12 image = (ImageView)findViewById(R.id.image);
    13 btn = (Button)findViewById(R.id.btn);
    14
    15 //获取上下文
    16 Context ctx = MainActivity.this;
    17 //获取ContentResolver对象
    18 ContentResolver resolver = ctx.getContentResolver();
    19 //获得外部存储卡上的图片缩略图
    20 Cursor c = resolver.query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, null, null, null, null);
    21 //为了for循环性能优化,用一变量存储数据条数
    22 totalCount = c.getCount();
    23
    24 //将Cursor移动到第一位
    25 c.moveToFirst();
    26 //将缩略图数据添加到ArrayList中
    27 for(int i=0; i<totalCount; i++){
    28 int index = c.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA);
    29 String src = c.getString(index);
    30 imageSrcs.add(src);
    31 index = i;
    32 c.moveToNext();
    33 }
    34 //关闭游标
    35 c.close();
    36
    37 //点击按钮,切换图片
    38 btn.setOnClickListener(new OnClickListener(){
    39 @Override
    40 publicvoid onClick(View v) {
    41 String src = imageSrcs.get(index);
    42 image.setImageURI(Uri.parse(src));
    43 index ++;
    44 if(index == totalCount){
    45 index =0;
    46 }
    47 }
    48 });
    49 }
    50 }

      其运行结果如下:

      哈哈,又是显示了一张美女图片,这也算是枯燥编程中的一点调味吧。

      MediaStore中的存储的信息是通过MediaScannerService这个后台服务维护的,MediaScannerService在接受到系统开机(BOOT_COMPLETED)、媒体挂载(MEDIA_MOUNTED)和扫描指令(MEDIA_SCANNER_SCAN_FILE)广播信息时,即启动MediaScannerService中扫描的相关代码(MediaScanner,此类被@hide隐藏,所以不多介绍)进行扫描和更新MediaStore内的信息。

      通过MediaStore和MediaScannerService的配合使用,实现类似系统自带的图库和音乐播放器文件列表功能。

    相关链接:

      Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (四) —— ContentProvider

  • 相关阅读:
    移动开发 Native APP、Hybrid APP和Web APP介绍
    urllib与urllib2的学习总结(python2.7.X)
    fiddler及postman讲解
    接口测试基础
    UiAutomator2.0 和1.x 的区别
    adb shell am instrument 命令详解
    GT问题记录
    HDU 2492 Ping pong (树状数组)
    CF 567C Geometric Progression
    CF 545E Paths and Trees
  • 原文地址:https://www.cnblogs.com/sode/p/2879753.html
Copyright © 2011-2022 走看看