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


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

Android系统提供的 ContentProvide  

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

Browser  浏览器

CallLog   储存通话记录

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

package c.example.jreduch09.contentprovider;

import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import c.example.jreduch09.R;

public class ContentActivity extends AppCompatActivity {
private Button img,mp3,mp4;
    private TextView show;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content);
        img=(Button)findViewById(R.id.img);
        mp3=(Button)findViewById(R.id.mp3);
        mp4=(Button)findViewById(R.id.mp4);
        show=(TextView)findViewById(R.id.show);
        img.setOnClickListener(new MyListener());
        mp3.setOnClickListener(new MyListener());
        mp4.setOnClickListener(new MyListener());
    }

    public  class  MyListener implements View.OnClickListener{
        @Override
        public void onClick(View view) {
            int id=view.getId();
            if (id==R.id.img){
                String str[]={MediaStore.Images.Media._ID,
                MediaStore.Images.Media.DISPLAY_NAME,
                MediaStore.Images.Media.DATA,
             };
                Cursor cursor=getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        str,null,null,null);
                StringBuilder sbd=new StringBuilder();
                while (cursor.moveToNext()){
                    sbd.append(cursor.getString(0)+":");
                    sbd.append(cursor.getString(1)+":");
                    sbd.append(cursor.getString(2)+"\n");
                }
                show.setText(sbd.toString());
            }else   if (id==R.id.mp3){
                String str[]={MediaStore.Audio.Media._ID,
                        MediaStore.Audio.Media.DISPLAY_NAME,//歌名
                        MediaStore.Audio.Media.DATA,         //路径
                        MediaStore.Audio.Media.SIZE,          //
                        MediaStore.Audio.Media.ARTIST,  //歌手
                        MediaStore.Audio.Media.DURATION,  //时长
                        MediaStore.Audio.Media.ALBUM,  //专辑

                };

       Cursor cursor=getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                 str,"duration>?",
               new String[]{"50000"},null );
                StringBuilder sbd=new StringBuilder();
                while (cursor.moveToNext()){
                    sbd.append(cursor.getString(0)+":");
                    sbd.append(cursor.getString(1)+":");
                    sbd.append(cursor.getString(2)+":");
                    sbd.append(cursor.getString(3)+":");
                    sbd.append(cursor.getString(4)+":");
                    sbd.append(cursor.getString(5)+":");
                    sbd.append(cursor.getString(6)+"\n");
                }
                show.setText(sbd.toString());
            }else   if (id==R.id.mp4){
                String str[]={MediaStore.Video.Media._ID,
                        MediaStore.Video.Media.DISPLAY_NAME,
                        MediaStore.Video.Media.DATA,
                        MediaStore.Video.Media.SIZE
                };

                Cursor cursor=getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                        str,null,null,null );
                StringBuilder sbd=new StringBuilder();
                while (cursor.moveToNext()){
                    sbd.append(cursor.getString(0)+":");
                    sbd.append(cursor.getString(1)+":");
                    sbd.append(cursor.getString(2)+":");
                    sbd.append(cursor.getString(3)+"\n");
                }
                show.setText(sbd.toString());

            }
        }
    }

}


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context="c.example.jreduch09.contentprovider.ContentActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/img"
        android:text="扫描图库"
        android:textSize="25sp"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/mp3"
        android:text="扫描音频"
        android:textSize="25sp"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/mp4"
        android:text="扫描视频"
        android:textSize="25sp"
        />
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/show"
            />
    </ScrollView>
</LinearLayout>

</RelativeLayout>







Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐