KitKat ANR访问空联系人照片时

时间:2013-11-28 11:27:18

标签: java android contactscontract

对于我的应用程序,我从ContactsContract API中读取了联系人的照片,该照片在Android KitKat之前运行良好。我找不到有关4.4更改日志中ContactsContract.Contacts.openContactPhotoInputStream()方法更改的具体内容,但我只能在KitKat上复制此内容,并且当该联系人没有照片时。

我使用以下内容阅读了联系人:

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;
}

此方法的调用如下:

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);
        }
    }
}

这将会持续几秒钟,但之后我会在LogCat中感到非常悲伤(通常超过50万行错误!?)

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)

我不确定为什么它现在说关闭流,因为这不会发生在任何其他版本的Android上。此外,如果它不在工作线程中,则仍会出现此错误。

2 个答案:

答案 0 :(得分:0)

您可以尝试对代码进行一些小改动吗?我遇到了类似的问题 - 我的应用只是停留在无限循环中。

试试这个:

InputStream photo_stream = ContactsContract.Contacts.openContactPhotoInputStream((ContentResolver) getContentResolver().query(ContactData, null, null, null, null),lookupUri, preferHighRes);

这对我有用。

答案 1 :(得分:0)

如果未设置联系人和照片,则必须使用默认联系人照片,您可以通过此路径找到该照片:

  

\ Android的SDK \平台\机器人-V#\数据\ RES \ drawable- \ ic_contact_picture.为png