public void handleMessage(Message msg) { WorkerArgs args = (WorkerArgs) msg.obj; Uri uri = null; if (msg.arg1 == EVENT_LOAD_IMAGE) { PhotoViewTag photoTag = (PhotoViewTag) args.view.getTag(TAG_PHOTO_INFOS); if (photoTag != null && photoTag.uri != null) { uri = photoTag.uri; Log.d(THIS_FILE, "get : " + uri); Bitmap img = contactsWrapper.getContactPhoto(args.context, uri, args.defaultResource); if (img != null) { args.result = img; } else { args.result = null; } } } else if (msg.arg1 == EVENT_LOAD_IMAGE_URI) { PhotoViewTag photoTag = (PhotoViewTag) args.view.getTag(TAG_PHOTO_INFOS); if (photoTag != null && photoTag.uri != null) { uri = photoTag.uri; Log.d(THIS_FILE, "get : " + uri); byte[] buffer = new byte[1024 * 16]; Bitmap img = null; try { InputStream is = args.context.getContentResolver().openInputStream(uri); if (is != null) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { int size; while ((size = is.read(buffer)) != -1) { baos.write(buffer, 0, size); } } finally { is.close(); } byte[] boasBytes = baos.toByteArray(); img = BitmapFactory.decodeByteArray(boasBytes, 0, boasBytes.length, null); } } catch (Exception ex) { Log.v(THIS_FILE, "Cannot load photo " + uri, ex); } if (img != null) { args.result = img; } else { args.result = null; } } } args.loadedUri = uri; // send the reply to the enclosing class. Message reply = ContactsAsyncHelper.this.obtainMessage(msg.what); reply.arg1 = msg.arg1; reply.obj = msg.obj; reply.sendToTarget(); }
public static void updateImageViewWithContactPhotoAsync( Context context, ImageView imageView, Uri photoUri, int placeholderImageResource) { if (sThreadHandler == null) { Log.d(THIS_FILE, "Update image view with contact async"); new ContactsAsyncHelper(); } // in case the source caller info is null, the URI will be null as well. // just update using the placeholder image in this case. if (photoUri == null) { defaultImage(imageView, placeholderImageResource); return; } if (isAlreadyProcessed(imageView, photoUri)) { return; } // Added additional Cookie field in the callee to handle arguments // sent to the callback function. // setup arguments WorkerArgs args = new WorkerArgs(); args.context = context; args.view = imageView; PhotoViewTag photoTag = new PhotoViewTag(); photoTag.uri = photoUri; args.view.setTag(TAG_PHOTO_INFOS, photoTag); args.defaultResource = placeholderImageResource; // setup message arguments Message msg = sThreadHandler.obtainMessage(); msg.arg1 = EVENT_LOAD_IMAGE_URI; msg.obj = args; preloadImage(imageView, placeholderImageResource, msg); }
/** * Start an image load, attach the result to the specified CallerInfo object. Note, when the query * is started, we make the ImageView INVISIBLE if the placeholderImageResource value is -1. When * we're given a valid (!= -1) placeholderImageResource value, we make sure the image is visible. */ public static final void updateImageViewWithContactPhotoAsync( int token, OnImageLoadCompleteListener listener, Object cookie, Context context, ImageView imageView, CallerInfo callerInfo, int placeholderImageResource) { if (sThreadHandler == null) { Log.d(THIS_FILE, "Update image view with contact async"); new ContactsAsyncHelper(); } // in case the source caller info is null, the URI will be null as well. // just update using the placeholder image in this case. if (callerInfo == null || callerInfo.contactContentUri == null) { defaultImage(imageView, placeholderImageResource); return; } // Check that the view is not already loading for same uri if (isAlreadyProcessed(imageView, callerInfo.contactContentUri)) { return; } // Added additional Cookie field in the callee to handle arguments // sent to the callback function. // setup arguments WorkerArgs args = new WorkerArgs(); args.cookie = cookie; args.context = context; args.view = imageView; PhotoViewTag photoTag = new PhotoViewTag(); photoTag.uri = callerInfo.contactContentUri; args.view.setTag(TAG_PHOTO_INFOS, photoTag); args.defaultResource = placeholderImageResource; args.listener = listener; // setup message arguments Message msg = sThreadHandler.obtainMessage(token); msg.arg1 = EVENT_LOAD_IMAGE; msg.obj = args; preloadImage(imageView, placeholderImageResource, msg); }