Created
April 12, 2016 18:23
-
-
Save lujop/efec556a1c52bc1601284fda239cb176 to your computer and use it in GitHub Desktop.
Glide loader to load contacts images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cat.joanpujol.utils.glide; | |
import android.content.Context; | |
import android.database.Cursor; | |
import android.net.Uri; | |
import android.provider.ContactsContract; | |
import com.bumptech.glide.Priority; | |
import com.bumptech.glide.load.data.DataFetcher; | |
import com.bumptech.glide.load.model.ModelLoader; | |
import com.bumptech.glide.load.model.stream.StreamModelLoader; | |
import java.io.ByteArrayInputStream; | |
import java.io.InputStream; | |
import de.greenrobot.common.io.IoUtils; | |
import timber.log.Timber; | |
/** | |
* Created by lujop on 11/04/16. | |
*/ | |
public class ContactsGlideLoader implements StreamModelLoader<Integer> { | |
private static volatile boolean photoSizeInitialized = false; | |
private static int photoSize = 96; | |
private static int thumbnailSize = 96; | |
private Context context; | |
private ModelLoader<Integer,InputStream> modelLoader; | |
public ContactsGlideLoader(Context context) { | |
this.context = context; | |
if(!photoSizeInitialized) | |
initContactsPhotoSize(context); | |
} | |
@Override | |
public ContactDataFetcher getResourceFetcher(Integer contactId, int width, int height) { | |
Uri contactUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, "" + contactId); | |
boolean thumbnail = width <= thumbnailSize; | |
return new ContactDataFetcher(contactUri,thumbnail,context); | |
} | |
public synchronized static void initContactsPhotoSize(final Context context) { | |
if(photoSizeInitialized) | |
return; | |
int phothoSize = 96; //Default; | |
final Uri uri = ContactsContract.DisplayPhoto.CONTENT_MAX_DIMENSIONS_URI; | |
final String[] projection = new String[] { ContactsContract.DisplayPhoto.DISPLAY_MAX_DIM, ContactsContract.DisplayPhoto.THUMBNAIL_MAX_DIM }; | |
final Cursor c = context.getContentResolver().query(uri, projection, null, null, null); | |
try { | |
c.moveToFirst(); | |
photoSize = c.getInt(0); | |
thumbnailSize = c.getInt(1); | |
photoSizeInitialized = true; | |
Timber.d("Contacts photo size initialized. Photo=%d Thumbnails=%d",phothoSize,thumbnailSize); | |
} finally { | |
c.close(); | |
} | |
} | |
static class ContactDataFetcher implements DataFetcher<InputStream> { | |
private boolean cancelled; | |
private Uri contactUri; | |
private Context context; | |
private boolean thumbnail; | |
public ContactDataFetcher(Uri contactUri, boolean thumbnail, Context context) { | |
this.contactUri = contactUri; | |
this.thumbnail = thumbnail; | |
this.context = context; | |
} | |
@Override | |
public InputStream loadData(Priority priority) throws Exception { | |
Timber.v("load contact photo for %s. Thumbnail=%b",contactUri,thumbnail); | |
InputStream stream = null; | |
if(contactUri!=null && !cancelled) { | |
if(thumbnail) | |
stream = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(), contactUri); | |
else | |
stream = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(), contactUri,true); | |
} | |
if(stream!=null) { | |
if(!cancelled) { | |
byte[] image = IoUtils.readAllBytes(stream); | |
stream = new ByteArrayInputStream(image); | |
} else { | |
stream.close(); | |
stream = null; | |
} | |
} | |
return stream; | |
} | |
@Override | |
public void cleanup() { | |
} | |
@Override | |
public String getId() { | |
return contactUri.toString()+"_thumbnail"+thumbnail; | |
} | |
@Override | |
public void cancel() { | |
cancelled = true; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
not work Glide 4.1.1 not found StreamModelLoader