コード例 #1
0
 @Override
 public void onPause() {
   super.onPause();
   // In the case onPause() is called during a fling the image loader is un-paused to let any
   // remaining background work complete.
   mImageLoader.setPauseWork(false);
 }
コード例 #2
0
  /**
   * Decodes and scales a contact's image from a file pointed to by a Uri in the contact's data, and
   * returns the result as a Bitmap. The column that contains the Uri varies according to the
   * platform version.
   *
   * @param photoData provide the Contact.PHOTO_THUMBNAIL_URI value.
   * @param imageSize The desired target width and height of the output image in pixels.
   * @return A Bitmap containing the contact's image, resized to fit the provided image size. If no
   *     thumbnail exists, returns null.
   */
  private Bitmap loadContactPhotoThumbnail(String photoData, int imageSize) {
    // Ensures the Fragment is still added to an activity. As this method is called in a
    // background thread, there's the possibility the Fragment is no longer attached and
    // added to an activity. If so, no need to spend resources loading the contact photo.
    if (!isAdded() || getActivity() == null) {
      return null;
    }

    // Instantiates an AssetFileDescriptor. Given a content Uri pointing to an image file, the
    // ContentResolver
    // can return an AssetFileDescriptor for the file.
    AssetFileDescriptor afd = null;

    // This "try" block catches an Exception if the file descriptor returned from the Contacts
    // Provider doesn't point to an existing file.
    try {
      Uri thumbUri;

      // Converts the Uri passed as a string to a Uri object.
      thumbUri = Uri.parse(photoData);

      // Retrieves a file descriptor from the Contacts Provider. To learn more about this
      // feature, read the reference documentation for
      // ContentResolver#openAssetFileDescriptor.
      afd = getActivity().getContentResolver().openAssetFileDescriptor(thumbUri, "r");

      // Gets a FileDescriptor from the AssetFileDescriptor. A BitmapFactory object can
      // decode the contents of a file pointed to by a FileDescriptor into a Bitmap.
      FileDescriptor fileDescriptor = afd.getFileDescriptor();

      if (fileDescriptor != null) {
        // Decodes a Bitmap from the image pointed to by the FileDescriptor, and scales it
        // to the specified width and height
        return ImageLoader.decodeSampledBitmapFromDescriptor(fileDescriptor, imageSize, imageSize);
      }
    } catch (FileNotFoundException e) {
      // If the file pointed to by the thumbnail URI doesn't exist, or the file can't be
      // opened in "read" mode, ContentResolver.openAssetFileDescriptor throws a
      // FileNotFoundException.
      if (BuildConfig.DEBUG) {
        Log.d(
            TAG,
            "Contact photo thumbnail not found for contact " + photoData + ": " + e.toString());
      }
    } finally {
      // If an AssetFileDescriptor was returned, try to close it
      if (afd != null) {
        try {
          afd.close();
        } catch (IOException e) {
          // Closing a file descriptor might cause an IOException if the file is
          // already closed. Nothing extra is needed to handle this.
        }
      }
    }

    // If the decoding failed, returns null
    return null;
  }
コード例 #3
0
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Let this fragment contribute menu items
    setHasOptionsMenu(true);

    // Create the main contacts adapter
    mAdapter = new ContactsAdapter(getActivity());

    // If we're restoring state after this fragment was recreated then retrieve previous search term
    // and previously selected search result.
    if (savedInstanceState != null) {
      mSearchTerm = savedInstanceState.getString(SearchManager.QUERY);
      mPreviouslySelectedSearchItem = savedInstanceState.getInt(STATE_PREVIOUSLY_SELECTED_KEY, 0);
    }

    /*
     * An ImageLoader object loads and resizes an image in the background and binds it to the
     * QuickContactBadge in each item layout of the ListView. ImageLoader implements memory
     * caching for each image, which substantially improves refreshes of the ListView as the
     * user scrolls through it.
     *
     * To learn more about downloading images asynchronously and caching the results, read the
     * Android training class Displaying Bitmaps Efficiently.
     *
     * http://developer.android.com/training/displaying-bitmaps/
     */
    mImageLoader =
        new ImageLoader(getActivity(), getListPreferredItemHeight()) {
          @Override
          protected Bitmap processBitmap(Object data) {
            // This gets called in a background thread and passed the data from
            // ImageLoader.loadImage().
            return loadContactPhotoThumbnail((String) data, getImageSize());
          }
        };

    // Set a placeholder loading image for the image loader
    mImageLoader.setLoadingImage(R.drawable.ic_contact_picture_holo_light);

    // Add a cache to the image loader
    mImageLoader.addImageCache(getActivity().getSupportFragmentManager(), 0.1f);
  }