contentprovider与resolver使用

注册provider:
<provider

    android:name=".MyContentProvider"

    android:authorities="com.example.mycontentprovider.mycontentprovider"  //唯一标识

    android:enabled="true"

android:exported="true"/>

provider获取数据:

public static void getContactsInfo(Context context) {

        //1. 获取内容解析者

        ContentResolver contentResolver = context.getContentResolver();

        // ContactsContract 联系人的API

        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;

        String[] projection = new String[]{

                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,

                ContactsContract.CommonDataKinds.Phone.NUMBER,

                ContactsContract.CommonDataKinds.Phone.CONTACT_ID

        };

        //2.查询操作

        //uri               :查询地址

        //projection        :查询的数据字段名称

        //selection         :查询的条件 where id=..

        //selectionArgs     :查询条件的参数

        //sortOrder         :排序s

//        contentResolver.query(uri, projection, selection, selectionArgs, sortOrder);

       Cursor cursor = contentResolver.query(uri, projection, null, null, null);

        while (cursor.moveToNext()) {

            String name = cursor.getString(0);

            String number = cursor.getString(1);

            int id = cursor.getInt(2);

           Bitmap bitmap = getContactPhoto(context, id);

            if (null == bitmap) {

                Log.e("Contact", "id: " + id + " 头像不为空");

            } else {

                Log.e("Contact", "id: " + id + " 头像为空");

            }

        }

    }

    public static Bitmap getContactPhoto(Context context, int id) {

        ContentResolver contentResolver = context.getContentResolver();

//        Uri uri = ContactsContract.Contacts.CONTENT_URI;

        //拼接路径

        //http://www.baidu.com/jdk

        //参数一:表的路径

        //参数二:联系人具体的路径

        Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, id + "");

        //获取联系人头像,以流的方式返回

        InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(contentResolver, uri);

        Bitmap bitmap = BitmapFactory.decodeStream(is);

        if (is != null) {

            try {

                is.close();

                is = null;

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

        return bitmap;

    }

Resolver调用:

ContentResolver resolver = context.getContentResolver();

                    resolver.delete(

                            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,

                            MediaStore.Images.Media.DISPLAY_NAME + "='" + originFileName + "." + photo.extensionName + "'",

                            null

                    );

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值