Back to project page AndroidImageCache.
The source code is released under:
Przemys?aw Jakubczyk Polidea Sp. z o.o. Copyright (c) 2012 All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the follo...
If you think the Android project AndroidImageCache listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/** */*w w w . j a v a 2 s . co m*/ */ package pl.polidea.webimageview.net; import android.content.Context; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.concurrent.ExecutorService; import pl.polidea.utils.StackPoolExecutor; import pl.polidea.utils.TempFile; /** * @author Przemys?aw Jakubczyk <przemyslaw.jakubczyk@pl.polidea.pl> */ public class WebClient { final File cacheDir; WebInterface webInterface = new WebInterfaceImpl(); ExecutorService taskExecutor = new StackPoolExecutor(10, 30); WebClient(final Context context) { cacheDir = context.getCacheDir(); } /** * Request for image. If Image under path url is downloading the current * time the listener is added to collection of callbacks. Otherwise new * download thread is run. * * @param url * the path * @param webCallback * the client result listener */ public void requestForImage(final String url, final WebCallback webCallback) { if (webCallback == null) { throw new IllegalArgumentException("webCallback cannot be null"); } taskExecutor.submit(buildTask(url, webCallback)); } /** * @param httpClient */ public void setWebInterface(final WebInterface httpClient) { if (httpClient != null) { this.webInterface = httpClient; } } public void setTaskExecutor(final ExecutorService taskExecutor) { this.taskExecutor = taskExecutor; } DownloadTask buildTask(String url, WebCallback webCallback) { return new DownloadTask(url, webCallback); } class DownloadTask implements Runnable { private final String url; private WebCallback webCallback; public DownloadTask(String url, WebCallback webCallback) { this.url = url; this.webCallback = webCallback; } @Override public void run() { TempFile tempFile = TempFile.nullObject(); try { tempFile = TempFile.createInDir(cacheDir); final InputStream stream = webInterface.execute(url); saveStreamToFile(stream, tempFile); webCallback.onWebHit(url, tempFile.asJavaFile()); } catch (final IOException e) { webCallback.onWebMiss(url); } finally { tempFile.delete(); } } public void saveStreamToFile(final InputStream is, final TempFile file) throws IOException { if (is == null) { throw new IOException("Empty stream"); } final BufferedOutputStream os = new BufferedOutputStream( new FileOutputStream(file.asJavaFile())); final byte[] data = new byte[16384]; int n; while ((n = is.read(data, 0, data.length)) != -1) { os.write(data, 0, n); } os.flush(); os.close(); } } }