简体   繁体   中英

How to get contact URI from user phone and load using glide in android

My requirement is i need to load contact image all the time using glide by fetching from user phone, what i have achieved so far is below:

Below method for getting contact URI with help of phone or email id:

public static Uri getContactURI(Context context, String emailOrMobile) {
    Log.v(TAG, "displayContactPic: " + emailOrMobile);

    if (emailOrMobile == null) {
        Log.w(TAG, "emailOrMobile: " + emailOrMobile);
        return null;
    }

    if (PermissionUtil.checkSelfPermission(context, Manifest.permission.READ_CONTACTS)
            != PackageManager.PERMISSION_GRANTED) {
        Log.w(TAG, "permission.READ_CONTACTS is not available! will skip");
        return null;
    }

    Long contactId = null;
    if (emailOrMobile.contains("@")) {
        contactId = AppUtils.getContactIdByEmail(emailOrMobile, context);
    } else {
        contactId = AppUtils.getContactIdByPhoneNums(emailOrMobile, context);
    }

    Log.v(TAG, "contactId: " + contactId);

    if (contactId != 0) {
        return Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, "" + contactId);
    } else {
        return null;
    }
}

pass getContactURI(contactImgUri) to below method for loading pic using glide like below

Glide.with(context)
     .load(contactImgUri)
     .apply(RequestOptions.circleCropTransform())
     .listener(new RequestListener<Drawable>() {
         @Override
         public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
             //Log.d(TAG, " onLoadFailed Exception: " + e);
             return false;
         }

         @Override
         public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, com.bumptech.glide.load.DataSource dataSource, boolean isFirstResource) {
             return false;
         }

     })
     .into(imageView);

Image is getting loaded but in android studio log-cat i see too many logs getting printed like below:

W/Glide: Load failed for content://com.android.contacts/contacts/513 with size [88x88] class com.bumptech.glide.load.engine.GlideException: Failed to load resource There were 3 causes: Glide: Root cause (1 of 3) java.io.FileNotFoundException: InputStream is null for content://com.android.contacts/contacts/513

Glide: Root cause (2 of 3) java.io.FileNotFoundException: Stream I/O not supported on this URI.; URI: content://com.android.contacts/contacts/513, calling user: android.uid.shared:10041, calling package is one of: [com.android.providers.blockednumber, com.android.providers.userdictionary, com.android.providers.contacts, com.android.calllogbackup] at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:149)

Glide: Root cause (3 of 3) java.io.FileNotFoundException: Stream I/O not supported on this URI.; URI: content://com.android.contacts/contacts/513, calling user: android.uid.shared:10041, calling package is one of: [com.android.providers.blockednumber, com.android.providers.userdictionary, com.android.providers.contacts, com.android.calllogbackup] at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:149)

if i have 1000+ contacts and in log-cat i can see too many such logs getting printed , im looking for a solution for avoiding logs or warning like above .

Any help is appreciated!

found below solution as below  
InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(), contactImgUri);
            if (is == null) {
                Log.d(TAG,"contact pic not readable");
                // Your contact doesn't have a valid photo
                // i.e. use the default photo for your app
            
                }
            } else {
                Log.d(TAG,"contact pic readable");
                // This will always succeed when assigned to an ImageView!
                  photoUri = Uri.withAppendedPath(contactImgUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);

                Glide.with(context)
                        .load(photoUri)
                        .apply(RequestOptions.circleCropTransform())
                        .listener(new RequestListener<Drawable>() {
                            @Override
                            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                                //Log.d(TAG, " onLoadFailed Exception: " + e);
                                return false;
                            }

                            @Override
                            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, com.bumptech.glide.load.DataSource dataSource, boolean isFirstResource) {
                               
                                return false;
                            }

                        })
                        .placeholder(R.drawable.ic_person)
                        .error(R.drawable.ic_person)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .skipMemoryCache(true)
                        .into(imageView);
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM