Android——ContentProvide 内容提供者+四大组件之三

Android提供的用于不同应用共享数据API

Android系统提供的 ContentProvide  

1Applications 提供已安装应用程序信息

Browser  浏览器

CallLog   储存通话记录

.........................

[java]  view plain  copy
  1. package c.example.jreduch09.contentprovider;  
  2.   
  3. import android.database.Cursor;  
  4. import android.os.Bundle;  
  5. import android.provider.MediaStore;  
  6. import android.support.v7.app.AppCompatActivity;  
  7. import android.view.View;  
  8. import android.widget.Button;  
  9. import android.widget.TextView;  
  10.   
  11. import c.example.jreduch09.R;  
  12.   
  13. public class ContentActivity extends AppCompatActivity {  
  14. private Button img,mp3,mp4;  
  15.     private TextView show;  
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_content);  
  20.         img=(Button)findViewById(R.id.img);  
  21.         mp3=(Button)findViewById(R.id.mp3);  
  22.         mp4=(Button)findViewById(R.id.mp4);  
  23.         show=(TextView)findViewById(R.id.show);  
  24.         img.setOnClickListener(new MyListener());  
  25.         mp3.setOnClickListener(new MyListener());  
  26.         mp4.setOnClickListener(new MyListener());  
  27.     }  
  28.   
  29.     public  class  MyListener implements View.OnClickListener{  
  30.         @Override  
  31.         public void onClick(View view) {  
  32.             int id=view.getId();  
  33.             if (id==R.id.img){  
  34.                 String str[]={MediaStore.Images.Media._ID,  
  35.                 MediaStore.Images.Media.DISPLAY_NAME,  
  36.                 MediaStore.Images.Media.DATA,  
  37.              };  
  38.                 Cursor cursor=getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,  
  39.                         str,null,null,null);  
  40.                 StringBuilder sbd=new StringBuilder();  
  41.                 while (cursor.moveToNext()){  
  42.                     sbd.append(cursor.getString(0)+":");  
  43.                     sbd.append(cursor.getString(1)+":");  
  44.                     sbd.append(cursor.getString(2)+"\n");  
  45.                 }  
  46.                 show.setText(sbd.toString());  
  47.             }else   if (id==R.id.mp3){  
  48.                 String str[]={MediaStore.Audio.Media._ID,  
  49.                         MediaStore.Audio.Media.DISPLAY_NAME,//歌名  
  50.                         MediaStore.Audio.Media.DATA,         //路径  
  51.                         MediaStore.Audio.Media.SIZE,          //  
  52.                         MediaStore.Audio.Media.ARTIST,  //歌手  
  53.                         MediaStore.Audio.Media.DURATION,  //时长  
  54.                         MediaStore.Audio.Media.ALBUM,  //专辑  
  55.   
  56.                 };  
  57.   
  58.        Cursor cursor=getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,  
  59.                  str,"duration>?",  
  60.                new String[]{"50000"},null );  
  61.                 StringBuilder sbd=new StringBuilder();  
  62.                 while (cursor.moveToNext()){  
  63.                     sbd.append(cursor.getString(0)+":");  
  64.                     sbd.append(cursor.getString(1)+":");  
  65.                     sbd.append(cursor.getString(2)+":");  
  66.                     sbd.append(cursor.getString(3)+":");  
  67.                     sbd.append(cursor.getString(4)+":");  
  68.                     sbd.append(cursor.getString(5)+":");  
  69.                     sbd.append(cursor.getString(6)+"\n");  
  70.                 }  
  71.                 show.setText(sbd.toString());  
  72.             }else   if (id==R.id.mp4){  
  73.                 String str[]={MediaStore.Video.Media._ID,  
  74.                         MediaStore.Video.Media.DISPLAY_NAME,  
  75.                         MediaStore.Video.Media.DATA,  
  76.                         MediaStore.Video.Media.SIZE  
  77.                 };  
  78.   
  79.                 Cursor cursor=getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,  
  80.                         str,null,null,null );  
  81.                 StringBuilder sbd=new StringBuilder();  
  82.                 while (cursor.moveToNext()){  
  83.                     sbd.append(cursor.getString(0)+":");  
  84.                     sbd.append(cursor.getString(1)+":");  
  85.                     sbd.append(cursor.getString(2)+":");  
  86.                     sbd.append(cursor.getString(3)+"\n");  
  87.                 }  
  88.                 show.setText(sbd.toString());  
  89.   
  90.             }  
  91.         }  
  92.     }  
  93.   
  94. }  


[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.   
  7.     tools:context="c.example.jreduch09.contentprovider.ContentActivity">  
  8. <LinearLayout  
  9.     android:layout_width="match_parent"  
  10.     android:layout_height="match_parent"  
  11.     android:orientation="vertical"  
  12.     >  
  13.   
  14.     <Button  
  15.         android:layout_width="match_parent"  
  16.         android:layout_height="50dp"  
  17.         android:id="@+id/img"  
  18.         android:text="扫描图库"  
  19.         android:textSize="25sp"  
  20.         />  
  21.     <Button  
  22.         android:layout_width="match_parent"  
  23.         android:layout_height="50dp"  
  24.         android:id="@+id/mp3"  
  25.         android:text="扫描音频"  
  26.         android:textSize="25sp"  
  27.         />  
  28.     <Button  
  29.         android:layout_width="match_parent"  
  30.         android:layout_height="50dp"  
  31.         android:id="@+id/mp4"  
  32.         android:text="扫描视频"  
  33.         android:textSize="25sp"  
  34.         />  
  35.     <ScrollView  
  36.         android:layout_width="match_parent"  
  37.         android:layout_height="match_parent">  
  38.   
  39.         <TextView  
  40.             android:layout_width="match_parent"  
  41.             android:layout_height="match_parent"  
  42.             android:id="@+id/show"  
  43.             />  
  44.     </ScrollView>  
  45. </LinearLayout>  
  46.   
  47. </RelativeLayout>  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值