@Override public void handleMessage(Message msg) { final SyncResultsActivity activity = mActivity.get(); if (activity == null) { return; } final Handler handler = activity.mMainHandler; if (handler == null) { return; } final SyncMyPixPreferences prefs = new SyncMyPixPreferencesReal(activity.getApplicationContext()); String url = (String) msg.obj; if (url != null) { try { InputStream friend = null; String filename = Uri.parse(url).getLastPathSegment(); if (activity.mSdCache != null) { friend = activity.mSdCache.get(filename); } // cache miss if (friend == null) { friend = Utils.downloadPictureAsStream(url); } synchronized (this) { if (running && friend != null) { byte[] bytes = Utils.getByteArrayFromInputStream(friend); friend.close(); Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); if (prefs.getCache()) { activity.mSdCache.add(filename, bytes); } Message mainMsg = handler.obtainMessage(); mainMsg.obj = bitmap; mainMsg.what = msg.what; handler.sendMessage(mainMsg); if (!activity.mCache.contains(url)) { activity.mCache.add(url, bitmap, true); } } } } catch (UnknownHostException ex) { ex.printStackTrace(); handler.sendEmptyMessage(MainHandler.UNKNOWN_HOST_ERROR); } catch (Exception e) { e.printStackTrace(); } } }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode != Activity.RESULT_OK) { return; } Uri contactData; switch (requestCode) { case PICK_CONTACT: contactData = data.getData(); updateContactWithSelection(contactData); break; case REQUEST_CROP_PHOTO: if (mUriOfSelected == null) { break; } ContentResolver resolver = getContentResolver(); Cursor cursor = resolver.query( mUriOfSelected, new String[] {Results.CONTACT_ID, Results.PIC_URL, Results.LOOKUP_KEY}, null, null, null); ResultsListAdapter adapter = (ResultsListAdapter) mListview.getAdapter(); if (cursor.moveToFirst()) { String id = cursor.getString(cursor.getColumnIndex(Results.CONTACT_ID)); String url = cursor.getString(cursor.getColumnIndex(Results.PIC_URL)); String lookup = cursor.getString(cursor.getColumnIndex(Results.LOOKUP_KEY)); Bitmap bitmap = (Bitmap) data.getParcelableExtra("data"); if (bitmap != null) { byte[] bytes = Utils.bitmapToPNG(bitmap); mContactUtils.updatePhoto(getContentResolver(), bytes, id, true); mDbHelper.updateHashes(id, lookup, null, bytes); mCache.add(url, bitmap); adapter.notifyDataSetChanged(); } } cursor.close(); break; } }