For my app I read the contact's photo from the ContactsContract API which worked fine up until Android KitKat. I can't find anything specific about changes to the ContactsContract.Contacts.openContactPhotoInputStream() method in the 4.4 change log but I can only replicate this on KitKat and when there is no photo present for that contact.
I read from the contacts using this:
public static Bitmap getContactPhoto(final Uri lookupUri, final Context context, boolean preferHighRes)
throws IOException, OutOfMemoryError, NullPointerException{
InputStream photo_stream = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(),
lookupUri, preferHighRes);
BufferedInputStream input = new BufferedInputStream(photo_stream);
Bitmap contactImg = BitmapFactory.decodeStream(input);
input.close();
return contactImg;
}
This method is called like so:
private class retrieveContactPhoto extends AsyncTask<Void, Void, Bitmap> {
Uri contactUri;
ImageView imageView;
public retrieveContactPhoto(Uri contactUri, ImageView iView){
this.contactUri = contactUri;
this.imageView = iView;
}
@Override
protected void onPreExecute(){
imageView.setImageResource(R.drawable.friend_image_default);
}
@Override
protected Bitmap doInBackground(Void... params) {
Bitmap image;
try {
image = ContactLookup.getContactPhoto(contactUri, mContext, true);
} catch (Exception e) {
image = null;
}
return image;
}
@Override
protected void onPostExecute(Bitmap image) {
if(image!=null){
imageView.setImageBitmap(image);
}
}
}
This will work for a few seconds but after that I will get plenty of grief in the LogCat (Usually over half million lines of errors!?)
11-28 11:05:03.067: W/System.err(9507): java.io.IOException: BufferedInputStream is closed
11-28 11:05:03.077: W/System.err(9507): at java.io.BufferedInputStream.streamClosed(BufferedInputStream.java:118)
11-28 11:05:03.087: W/System.err(9507): at java.io.BufferedInputStream.read(BufferedInputStream.java:279)
11-28 11:05:03.097: W/System.err(9507): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
11-28 11:05:03.097: W/System.err(9507): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:719)
11-28 11:05:03.097: W/System.err(9507): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:791)
11-28 11:05:03.097: W/System.err(9507): at com.repay.android.addDebt.ContactLookup.getContactPhoto(ContactLookup.java:31)
11-28 11:05:03.097: W/System.err(9507): at com.repay.android.StartFragmentAdapter$retrieveContactPhoto.doInBackground(StartFragmentAdapter.java:110)
11-28 11:05:03.097: W/System.err(9507): at com.repay.android.StartFragmentAdapter$retrieveContactPhoto.doInBackground(StartFragmentAdapter.java:1)
11-28 11:05:03.097: W/System.err(9507): at android.os.AsyncTask$2.call(AsyncTask.java:264)
11-28 11:05:03.107: W/System.err(9507): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-28 11:05:03.107: W/System.err(9507): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-28 11:05:03.107: W/System.err(9507): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
11-28 11:05:03.117: W/System.err(9507): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-28 11:05:03.117: W/System.err(9507): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-28 11:05:03.117: W/System.err(9507): at java.lang.Thread.run(Thread.java:864)
I'm not sure why it now says about the stream being closed since this doesn't happen on any other version of Android. Also, this error will still appear if it's not in a worker thread.