Android Open Source - SandB-Android Body Image Getter






From Project

Back to project page SandB-Android.

License

The source code is released under:

GNU General Public License

If you think the Android project SandB-Android 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

package edu.grinnell.sandb.img;
//www  .  ja  v a 2 s. c  om
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.content.Context;
import android.os.AsyncTask;
import edu.grinnell.sandb.data.Article;

public class BodyImageGetter {

  private static ImageTable mImageTable;

  private static int numTasks = 0;

  public BodyImageGetter(Context context) {
    if (mImageTable == null)
      mImageTable = new ImageTable(context);
  }

  public void buildImageCache(Article article) {
    new ImageHelper().execute(article);
  }

  public class ImageHelper extends AsyncTask<Article, Integer, Integer> {

    @Override
    protected void onPreExecute() {
      if (numTasks++ == 0) {
        mImageTable.open();
        mImageTable.clearTable();
      }
    }

    @Override
    protected Integer doInBackground(Article... article) {

      String body = article[0].getBody();
      int articleId = article[0].getId();

      readImage(body, articleId);
      return null;
    }
  }

  protected void onPostExecute(Integer i) {
    if (--numTasks == 0)
      mImageTable.close();
  }

  // Read an image from the body as a byte array
  public void readImage(String body, int articleID) {

    addImage(body, articleID, "<div");
//    addImage(body, articleID, "<a");
    addImage(body, articleID, "<img");
  }

  private void addImage(String body, int articleId, String tag) {
    int tagStart = 0;
    String url = "";
    byte[] image = null;
    String title = "";

    while ((tagStart = body.indexOf(tag, tagStart + 1)) >= 0) {
      url = getSubstring("src=\"", body, tagStart);
      
      //check to make sure image has not yet been added
      if ((Image) mImageTable.findByUrl(url) != null)
        return;

      //image = getImage(url, tagStart);
      title = getSubstring("title=\"", body, tagStart);
      
      mImageTable.createImage(articleId, url, image, title);
    }

  }

  private static byte[] getImage(String imgSource, int start) {
    // download image as byte array

    ByteArrayOutputStream buffer = new ByteArrayOutputStream();

    int nRead;
    byte[] data = new byte[16384];

    InputStream is;
    try {
      is = fetch(imgSource);
      while ((nRead = is.read(data, 0, data.length)) != -1) {
        buffer.write(data, 0, nRead);
      }
      buffer.flush();
    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
      return null;
    }

    return buffer.toByteArray();

  }

  private static InputStream fetch(String urlString)
      throws IllegalArgumentException, MalformedURLException, IOException {

    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpGet request = new HttpGet(urlString);
    HttpResponse response = httpClient.execute(request);

    return response.getEntity().getContent();
  }

  // return a string starting immediately after the key, and ending at the
  // first quotation mark
  private static String getSubstring(String key, String body, int start) {
    int subStart = 0;
    int subEnd = 0;
    String substring = "";

    // start at beginning of link
    subStart = body.indexOf(key, start) + key.length();

    if (subStart >= 0) {
      subEnd = body.indexOf("\"", subStart);
    }
    
    if (subEnd >= subStart) {
      substring = body.substring(subStart, subEnd);
    }

    return substring;
  }
}




Java Source Code List

edu.grinnell.sandb.ArticleDetailActivity.java
edu.grinnell.sandb.ArticleDetailFragment.java
edu.grinnell.sandb.ArticleListAdapter.java
edu.grinnell.sandb.ArticleListFragment.java
edu.grinnell.sandb.CommentListAdapter.java
edu.grinnell.sandb.CommentListFragment.java
edu.grinnell.sandb.ImagePagerActivity.java
edu.grinnell.sandb.MainActivity.java
edu.grinnell.sandb.MainPrefs.java
edu.grinnell.sandb.ScarletAndBlackApplication.java
edu.grinnell.sandb.Utility.java
edu.grinnell.sandb.comments.CommentStorageHelper.java
edu.grinnell.sandb.comments.CommentTable.java
edu.grinnell.sandb.comments.Comment.java
edu.grinnell.sandb.data.ArticleStorageHelper.java
edu.grinnell.sandb.data.ArticleTable.java
edu.grinnell.sandb.data.Article.java
edu.grinnell.sandb.img.BodyImageGetter.java
edu.grinnell.sandb.img.ImageStorageHelper.java
edu.grinnell.sandb.img.ImageTable.java
edu.grinnell.sandb.img.Image.java
edu.grinnell.sandb.img.UniversalLoaderUtility.java
edu.grinnell.sandb.xmlpull.CommentParseTask.java
edu.grinnell.sandb.xmlpull.XmlCheckAgeTask.java
edu.grinnell.sandb.xmlpull.XmlFetchTask.java
edu.grinnell.sandb.xmlpull.XmlParseTask.java
edu.grinnell.sandb.xmlpull.XmlPullReceiver.java
edu.grinnell.sandb.xmlpull.XmlPullService.java