Android Open Source - mobile-shopping-assistant-appengine Image Url View Binder






From Project

Back to project page mobile-shopping-assistant-appengine.

License

The source code is released under:

Apache License

If you think the Android project mobile-shopping-assistant-appengine listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (c) 2012 Google Inc./*from  w ww.j a v  a  2 s  . c o m*/
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.samplesolutions.mobileassistant;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.view.View;
import android.widget.ImageView;
import android.widget.SimpleAdapter;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;

/**
 * Helper class for displaying images retrieved asynchronously from Internet locations.
 */
class ImageUrlViewBinder implements SimpleAdapter.ViewBinder {
  Set<Integer> imageViewIds;

  /**
   * @constructor
   *
   * @param imageViewIds set of resource ids of ImageViews that will have images downloaded from
   *        Internet
   */
  ImageUrlViewBinder(Set<Integer> imageViewIds) {
    this.imageViewIds = imageViewIds;
  }

  /**
   * @constructor
   *
   * @param imageViewId the resource id of ImageView that will have images downloaded from Internet
   */
  ImageUrlViewBinder(int imageViewId) {
    imageViewIds = new HashSet<Integer>();
    imageViewIds.add(imageViewId);
  }


  /**
   * If the view has been configured to display images downloaded from Internet, the method
   * interprets the data argument as an Url, downloads the image from that Url asynchronously and
   * binds it to the specified view.
   */
  @Override
  public boolean setViewValue(View view, Object data, String textRepresentation) {
    if (!imageViewIds.contains(view.getId())) {
      return false;
    }

    // TODO(ggogol): Implement local caching on the device
    String uri = (String) data;
    ImageView imageView = (ImageView) view;
    new DownloadImageAsyncTask(imageView, R.drawable.ic_launcher).execute(uri);
    return true;
  }
}


/**
 * AsyncTask that asynchronously downloads an image from an url and sets the image as a bitmap for a
 * specified ImageView
 */
class DownloadImageAsyncTask extends AsyncTask<String, Void, Bitmap> {
  private Logger log = Logger.getLogger(MainActivity.class.getName());
  ImageView imageView;
  int fallbackResId;

  /**
   * @constructor
   *
   * @param imageView imageView that will have the downloaded image set to
   * @param fallbackResId a bitmap resource id to be used when downloading the image from the url
   *        fails
   */
  public DownloadImageAsyncTask(ImageView imageView, int fallbackResId) {
    this.imageView = imageView;
    this.fallbackResId = fallbackResId;
  }

  /**
   * Downloads the image from Url and creates a bitmap out of it
   */
  @Override
  protected Bitmap doInBackground(String... urls) {
    String url = urls[0];
    Bitmap bitmap = null;
    try {
      InputStream in = new java.net.URL(url).openStream();
      bitmap = BitmapFactory.decodeStream(in);
    } catch (IOException e) {
      log.warning("Downloading image failed: " + e.getMessage());
    }
    return bitmap;
  }

  /**
   * If the download succeeded, it binds the downloaded bitmap to the view. Otherwise it binds a
   * static bitmap passed as fallbackResId to the constructor
   *
   */
  @Override
  protected void onPostExecute(Bitmap result) {
    if (result == null) {
      imageView.setImageResource(fallbackResId);
    } else {
      imageView.setImageBitmap(result);
    }
  }
}




Java Source Code List

ch.yereaztian.mobileassistant.CheckInEndpoint.java
ch.yereaztian.mobileassistant.CheckIn.java
ch.yereaztian.mobileassistant.CloudEndpointUtils.java
ch.yereaztian.mobileassistant.DeviceInfoEndpoint.java
ch.yereaztian.mobileassistant.DeviceInfo.java
ch.yereaztian.mobileassistant.EMF.java
ch.yereaztian.mobileassistant.GCMIntentService.java
ch.yereaztian.mobileassistant.ImageUrlViewBinder.java
ch.yereaztian.mobileassistant.MainActivity.java
ch.yereaztian.mobileassistant.MessageData.java
ch.yereaztian.mobileassistant.MessageEndpoint.java
ch.yereaztian.mobileassistant.OfferEndpoint.java
ch.yereaztian.mobileassistant.Offer.java
ch.yereaztian.mobileassistant.PlaceDetailsActivity.java
ch.yereaztian.mobileassistant.PlaceEndpoint.java
ch.yereaztian.mobileassistant.Place.java
ch.yereaztian.mobileassistant.RecommendationEndpoint.java
ch.yereaztian.mobileassistant.Recommendation.java
ch.yereaztian.mobileassistant.RegisterActivity.java
ch.yereaztian.mobileassistant.checkinendpoint.CheckinendpointRequestInitializer.java
ch.yereaztian.mobileassistant.checkinendpoint.CheckinendpointRequest.java
ch.yereaztian.mobileassistant.checkinendpoint.CheckinendpointScopes.java
ch.yereaztian.mobileassistant.checkinendpoint.Checkinendpoint.java
ch.yereaztian.mobileassistant.checkinendpoint.model.CheckIn.java
ch.yereaztian.mobileassistant.checkinendpoint.model.CollectionResponseCheckIn.java
ch.yereaztian.mobileassistant.checkinendpoint.model.Key.java
ch.yereaztian.mobileassistant.deviceinfoendpoint.DeviceinfoendpointRequestInitializer.java
ch.yereaztian.mobileassistant.deviceinfoendpoint.DeviceinfoendpointRequest.java
ch.yereaztian.mobileassistant.deviceinfoendpoint.DeviceinfoendpointScopes.java
ch.yereaztian.mobileassistant.deviceinfoendpoint.Deviceinfoendpoint.java
ch.yereaztian.mobileassistant.deviceinfoendpoint.model.CollectionResponseDeviceInfo.java
ch.yereaztian.mobileassistant.deviceinfoendpoint.model.DeviceInfo.java
ch.yereaztian.mobileassistant.messageEndpoint.MessageEndpointRequestInitializer.java
ch.yereaztian.mobileassistant.messageEndpoint.MessageEndpointRequest.java
ch.yereaztian.mobileassistant.messageEndpoint.MessageEndpointScopes.java
ch.yereaztian.mobileassistant.messageEndpoint.MessageEndpoint.java
ch.yereaztian.mobileassistant.messageEndpoint.model.CollectionResponseMessageData.java
ch.yereaztian.mobileassistant.messageEndpoint.model.Key.java
ch.yereaztian.mobileassistant.messageEndpoint.model.MessageData.java
ch.yereaztian.mobileassistant.offerendpoint.OfferendpointRequestInitializer.java
ch.yereaztian.mobileassistant.offerendpoint.OfferendpointRequest.java
ch.yereaztian.mobileassistant.offerendpoint.OfferendpointScopes.java
ch.yereaztian.mobileassistant.offerendpoint.Offerendpoint.java
ch.yereaztian.mobileassistant.offerendpoint.model.CollectionResponseOffer.java
ch.yereaztian.mobileassistant.offerendpoint.model.Offer.java
ch.yereaztian.mobileassistant.placeendpoint.PlaceendpointRequestInitializer.java
ch.yereaztian.mobileassistant.placeendpoint.PlaceendpointRequest.java
ch.yereaztian.mobileassistant.placeendpoint.PlaceendpointScopes.java
ch.yereaztian.mobileassistant.placeendpoint.Placeendpoint.java
ch.yereaztian.mobileassistant.placeendpoint.model.CollectionResponsePlace.java
ch.yereaztian.mobileassistant.placeendpoint.model.GeoPt.java
ch.yereaztian.mobileassistant.placeendpoint.model.Key.java
ch.yereaztian.mobileassistant.placeendpoint.model.Place.java
ch.yereaztian.mobileassistant.recommendationendpoint.RecommendationendpointRequestInitializer.java
ch.yereaztian.mobileassistant.recommendationendpoint.RecommendationendpointRequest.java
ch.yereaztian.mobileassistant.recommendationendpoint.RecommendationendpointScopes.java
ch.yereaztian.mobileassistant.recommendationendpoint.Recommendationendpoint.java
ch.yereaztian.mobileassistant.recommendationendpoint.model.CollectionResponseRecommendation.java
ch.yereaztian.mobileassistant.recommendationendpoint.model.Key.java
ch.yereaztian.mobileassistant.recommendationendpoint.model.Recommendation.java
com.google.samplesolutions.mobileassistant.ImageUrlViewBinder.java