Below is the code for imagedownloader which I am using. When a particular image is downloaded first time imagedownloader will download it and store it. For future references again I will pass the url. If image exists then it will be retrieved locally and if it does not exist it will be downloaded. This code functions properly in android 2.2 and 2.3.3. However when i ran it in emulator: android 4.0 and my note2 the image is always downloaded from the internet. It is not retrieved from local storage.
ImageDownloader.java
public class ImageDownloader {
Map<String,Bitmap> imageCache;
public ImageDownloader(){
imageCache = new HashMap<String, Bitmap>();
}
//download function
public void download(String url, ImageView imageView) {
if (cancelPotentialDownload(url, imageView)) {
//Caching code right here
String filename = String.valueOf(url.hashCode());
File f = new File(getCacheDirectory(imageView.getContext()), filename);
// Is the bitmap in our memory cache?
Bitmap bitmap = null;
bitmap = (Bitmap)imageCache.get(f.getPath());
if(bitmap == null){
bitmap = BitmapFactory.decodeFile(f.getPath());
if(bitmap != null){
imageCache.put(f.getPath(), bitmap);
}
}
//No? download it
if(bitmap == null){
BitmapDownloaderTask task = new BitmapDownloaderTask(imageView);
DownloadedDrawable downloadedDrawable = new DownloadedDrawable(task);
imageView.setImageDrawable(downloadedDrawable);
task.execute(url);
}else{
//Yes? set the image
imageView.setImageBitmap(bitmap);
}
}
}
//cancel a download (internal only)
private static boolean cancelPotentialDownload(String url, ImageView imageView) {
BitmapDownloaderTask bitmapDownloaderTask = getBitmapDownloaderTask(imageView);
if (bitmapDownloaderTask != null) {
String bitmapUrl = bitmapDownloaderTask.url;
if ((bitmapUrl == null) || (!bitmapUrl.equals(url))) {
bitmapDownloaderTask.cancel(true);
} else {
// The same URL is already being downloaded.
return false;
}
}
return true;
}
//gets an existing download if one exists for the imageview
private static BitmapDownloaderTask getBitmapDownloaderTask(ImageView imageView) {
if (imageView != null) {
Drawable drawable = imageView.getDrawable();
if (drawable instanceof DownloadedDrawable) {
DownloadedDrawable downloadedDrawable = (DownloadedDrawable)drawable;
return downloadedDrawable.getBitmapDownloaderTask();
}
}
return null;
}
//our caching functions
// Find the dir to save cached images
private static File getCacheDirectory(Context context){
String sdState = android.os.Environment.getExternalStorageState();
File cacheDir;
if (sdState.equals(android.os.Environment.MEDIA_MOUNTED)) {
File sdDir = android.os.Environment.getExternalStorageDirectory();
//TODO : Change your diretcory here
cacheDir = new File(sdDir,"data/tac/images");
Log.w("ImageDownloader", "Error while retrieving bitmap from ");
}
else
cacheDir = context.getCacheDir();
if(!cacheDir.exists())
cacheDir.mkdirs();
return cacheDir;
}
private void writeFile(Bitmap bmp, File f) {
FileOutputStream out = null;
try {
out = new FileOutputStream(f);
bmp.compress(Bitmap.CompressFormat.PNG, 80, out);
} catch (Exception e) {
e.printStackTrace();
}
finally {
try { if (out != null ) out.close(); }
catch(Exception ex) {}
}
}
///////////////////////
//download asynctask
public class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap> {
private String url;
private final WeakReference<ImageView> imageViewReference;
public BitmapDownloaderTask(ImageView imageView) {
imageViewReference = new WeakReference<ImageView>(imageView);
}
#Override
// Actual download method, run in the task thread
protected Bitmap doInBackground(String... params) {
// params comes from the execute() call: params[0] is the url.
url = (String)params[0];
return downloadBitmap(params[0]);
}
#Override
// Once the image is downloaded, associates it to the imageView
protected void onPostExecute(Bitmap bitmap) {
if (isCancelled()) {
bitmap = null;
}
if (imageViewReference != null) {
ImageView imageView = imageViewReference.get();
BitmapDownloaderTask bitmapDownloaderTask = getBitmapDownloaderTask(imageView);
// Change bitmap only if this process is still associated with it
if (this == bitmapDownloaderTask) {
imageView.setImageBitmap(bitmap);
//cache the image
String filename = String.valueOf(url.hashCode());
File f = new File(getCacheDirectory(imageView.getContext()), filename);
imageCache.put(f.getPath(), bitmap);
writeFile(bitmap, f);
}
}
}
}
static class DownloadedDrawable extends ColorDrawable {
private final WeakReference<BitmapDownloaderTask> bitmapDownloaderTaskReference;
public DownloadedDrawable(BitmapDownloaderTask bitmapDownloaderTask) {
super(Color.BLACK);
bitmapDownloaderTaskReference =
new WeakReference<BitmapDownloaderTask>(bitmapDownloaderTask);
}
public BitmapDownloaderTask getBitmapDownloaderTask() {
return bitmapDownloaderTaskReference.get();
}
}
//the actual download code
static Bitmap downloadBitmap(String url) {
HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpClient client = new DefaultHttpClient(params);
final HttpGet getRequest = new HttpGet(url);
try {
HttpResponse response = client.execute(getRequest);
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w("ImageDownloader", "Error " + statusCode + " while retrieving bitmap from " + url);
return null;
}
final HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = null;
try {
inputStream = entity.getContent();
final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
} finally {
if (inputStream != null) {
inputStream.close();
}
entity.consumeContent();
}
}
} catch (Exception e) {
// Could provide a more explicit error message for IOException or IllegalStateException
getRequest.abort();
Log.w("ImageDownloader", "Error while retrieving bitmap from " + url + e.toString());
} finally {
if (client != null) {
//client.close();
}
}
return null;
}
}
Use ShutterBug Library to download pic from HTTP.
https://github.com/applidium/Shutterbug
It just a one line code.
Thanks
Related
My json array contains two images, how to display that in activity? Those images are in the form of url. After and before are the imageviews, I want to know what is image download task and how can I use that.
after = (ImageView) findViewById(R.id.bfrdoogret);
before = (ImageView) findViewById(R.id.afterdogret);
}
class BackGround extends AsyncTask<String, String, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
dialog = ProgressDialog.show(Serialno.this, "",
"Searching. Please wait...", true);
dialog.setCancelable(true);
dialog.show();
}
#Override
protected String doInBackground(String... params)
{
String values=getvalues();
return values;
}
#Override
protected void onPostExecute(String s)
{
dialog.hide();
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_SHORT).show();
dis.setText(district);
city.setText(cit);
serialno.setText(serial_no);
gender.setText(gende);
loction.setText(latitude+ longitude);
}
}
public String getvalues()
{
String uri = "http://www.lorryguru.com/domains/dogs/retusingsno.php?district="+District+"&serialno="+SerialNo;
HttpGet httpGet = new HttpGet(uri);
HttpClient client = new DefaultHttpClient();
HttpResponse response;
StringBuilder stringBuilder = new StringBuilder();
try {
response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while ((b = stream.read()) != -1)
{
stringBuilder.append((char) b);
}
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
JSONArray arr = new JSONArray(stringBuilder.toString());
//result = jsonObj.getJSONArray(TAG_RESULT);
JSONObject c = arr.getJSONObject(0);
id = c.getString(TAG_ID);
district = c.getString(TAG_DISTRICT);
cit = c.getString(TAG_CITY);
serial_no = c.getString(TAG_SERIALNO);
gende = c.getString(TAG_GENDER);
wareno = c.getString(TAG_WARENO);
befor = c.getString(TAG_BEFORE);
afte = c.getString(TAG_AFTER);
latitude = c.getString(TAG_LATITUDE);
longitude = c.getString(TAG_LONGITUDE);
return "success";
}
catch (JSONException e)
{
return "accessproblem";
//e.printStackTrace();
}
Add this dependency in gradle compile 'com.squareup.picasso:picasso:2.5.2'
As per your getValues() method you will get url like this
String yourImageUrlHere= c.getString("whatever is your key");
Picasso.with(this)
.load(yourImageUrlHere)
.into(yourImageViewHere);
You have to add another async task after to your existing one, Code is following below.
class ImageDownloaderTask extends AsyncTask<String, Void, Bitmap>
{
private final WeakReference<ImageView> imageViewReference;
String murl;
ImageView showView;
public ImageDownloaderTask(ImageView imageView,String url) {
imageViewReference = new WeakReference<ImageView>(imageView);
murl=url;
showView=imageView;
}
#Override
protected Bitmap doInBackground(String... params) {
final Bitmap bitmap=downloadBitmap(murl);
if(murl!=null && bitmap!=null)
{
addBitmapToMemoryCache(murl, bitmap);
}
return bitmap;
}
#Override
protected void onProgressUpdate(Void... values) {
// TODO Auto-generated method stub
}
#Override
protected void onPostExecute(Bitmap bitmap) {
if (isCancelled()) {
bitmap = null;
}
if (imageViewReference != null) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
if (bitmap != null) {
imageView.setImageBitmap(bitmap);
//addBitmapToMemoryCache(murl, bitmap);
}
}
}
}
}
private Bitmap downloadBitmap(String url)
{
HttpURLConnection urlConnection = null;
Bitmap bitmap;
try {
URL uri = new URL(url);
urlConnection = (HttpURLConnection) uri.openConnection();
int statusCode = urlConnection.getResponseCode();
if (statusCode != HttpStatus.SC_OK)
{
return null;
}
InputStream inputStream = urlConnection.getInputStream();
bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
catch (Exception e)
{
urlConnection.disconnect();
Log.w("ImageDownloader", "Error downloading image from " + url);
}
finally
{
if (urlConnection != null) {
urlConnection.disconnect();
}
}
return null;
}
Use Picasso to download image asynchronously from url.
You can use Picasso:
Picasso.with(this)
.load(url)
.into(imageView);
My scenario is : I am creating a new bitmap and save to local path then display the image in SimpleDraweeView. If i am using SDV.setImageUri(path) sometimes image is not displayed. So i am using mImageView.setImageDrawable(new BitmapDrawable(mContext.getResources(), bitmap)); . If i am loading next time then ImageView is flickering at the time of loading. I am research about flickering that image is not available in cache that's why its occur. So how can i add image to fersco cache;
You can use fresco to cache existing bitmap
public class DownloadVideoThumbnail extends AsyncTask<String, Void, Bitmap> {
private ImageView bmImage;
private Bitmap bitmapVideo;
private Context context;
public DownloadVideoThumbnail(Context context, ImageView bmImage) {
this.bmImage = (ImageView) bmImage;
this.context = context;
}
protected Bitmap doInBackground(String... urls) {
String urlStr = urls[0];
if (readFromCacheSync(urlStr) == null) {
try {
//Your method call here
bitmapVideo = retriveVideoFrameFromVideo(urlStr);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
} else {
bitmapVideo = readFromCacheSync(urlStr);
}
return null;
}
protected void onPostExecute(Bitmap result) {
if (bitmapVideo != null) {
//Load your bitmap here
bmImage.setImageBitmap(bitmapVideo);
bmImage.setScaleType(ImageView.ScaleType.CENTER_CROP);
}
}
public void cacheBitmap(Bitmap bitmap, String url) {
try {
CacheKey cacheKey = new SimpleCacheKey(url);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
final byte[] byteArray = stream.toByteArray();
Fresco.getImagePipelineFactory().getMainFileCache().insert(cacheKey, new WriterCallback() {
#Override
public void write(OutputStream outputStream) throws IOException {
outputStream.write(byteArray);
}
});
} catch (IOException cacheWriteException) {
}
}
public static Bitmap readFromCacheSync(String imageUrl) {
CacheKey cacheKey = DefaultCacheKeyFactory.getInstance().getEncodedCacheKey(ImageRequest.fromUri(imageUrl), null);
StagingArea stagingArea = StagingArea.getInstance();
EncodedImage encodedImage = stagingArea.get(cacheKey);
if (encodedImage != null) {
return BitmapFactory.decodeStream(encodedImage.getInputStream());
}
try {
return BitmapFactory.decodeStream(readFromDiskCache(cacheKey));
} catch (Exception e) {
return null;
}
}
private static InputStream readFromDiskCache(final CacheKey key) throws IOException {
try {
FileCache fileCache = ImagePipelineFactory.getInstance().getMainFileCache();
final BinaryResource diskCacheResource = fileCache.getResource(key);
if (diskCacheResource == null) {
FLog.v(TAG, "Disk cache miss for %s", key.toString());
return null;
}
PooledByteBuffer byteBuffer;
final InputStream is = diskCacheResource.openStream();
FLog.v(TAG, "Successful read from disk cache for %s", key.toString());
return is;
} catch (IOException ioe) {
return null;
}
}
public Bitmap retriveVideoFrameFromVideo(String videoPath) throws Throwable {
Bitmap bitmap = null;
MediaMetadataRetriever mediaMetadataRetriever = null;
try {
mediaMetadataRetriever = new MediaMetadataRetriever();
if (Build.VERSION.SDK_INT >= 14)
mediaMetadataRetriever.setDataSource(videoPath, new HashMap<String, String>());
else
mediaMetadataRetriever.setDataSource(videoPath);
bitmap = mediaMetadataRetriever.getFrameAtTime();
if (bitmap != null) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream);
cacheBitmap(bitmap, videoPath);
}
} catch (Exception e) {
e.printStackTrace();
throw new Throwable(
"Exception in retriveVideoFrameFromVideo(String videoPath)"
+ e.getMessage());
} finally {
if (mediaMetadataRetriever != null) {
mediaMetadataRetriever.release();
}
}
return bitmap;
}
}
I am developing an Android application to download images from my web server. All the Code is running fine. I am using Asynctask to download the images to my sdcard.
I am on a 4mbps connection but my application is taking about 8 mins to download 3 images (2.5 MB). I have read else where that Asynctask automatically manages Thread creation, so now what I can do to achieve concurrency ?
I am posting my code below. The code Below is for my Asynctask activity that downloads the image from server to sdcard.
public class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap> {
private String url;
Bitmap bitmap1;
String sdCard;
private final WeakReference<ImageView> imageViewReference;
public BitmapDownloaderTask(ImageView imageView) {
imageViewReference = new WeakReference<ImageView>(imageView);
}
#Override
// Actual download method, run in the task thread
protected Bitmap doInBackground(String... params) {
// params comes from the execute() call: params[0] is the url.
bitmap1 = downloadBitmap(params[0]);
boolean avail = isMemorySizeAvailableAndroid(bitmap1.getRowBytes(),
Environment.isExternalStorageEmulated());
if (avail) {
try {
sdCard = Environment.getExternalStorageDirectory().toString()
+ "/MyCatalogue";
File f1 = new File(sdCard);
if (!f1.exists()) {
f1.mkdirs();
}
String filename1 = params[0].substring(params[0]
.lastIndexOf("/") + 1);
File file1 = new File(f1.toString(), filename1);
OutputStream stream1 = new FileOutputStream(file1);
bitmap1.compress(CompressFormat.JPEG, 100, stream1);
Log.w("Abhishek", "card is " + sdCard);
} catch (Exception e) {
e.printStackTrace();
}
}
Log.w("ImageDownloader", "Success bitmap is" + bitmap1);
return downloadBitmap(params[0]);
}
protected static boolean isMemorySizeAvailableAndroid(long download_bytes,
boolean isExternalMemory) {
boolean isMemoryAvailable = false;
long freeSpace = 0;
// if isExternalMemory get true to calculate external SD card available
// size
if (isExternalMemory) {
try {
StatFs stat = new StatFs(Environment
.getExternalStorageDirectory().getPath());
freeSpace = (long) stat.getAvailableBlocks()
* (long) stat.getBlockSize();
if (freeSpace > download_bytes) {
isMemoryAvailable = true;
} else {
isMemoryAvailable = false;
}
} catch (Exception e) {
e.printStackTrace();
isMemoryAvailable = false;
}
} else {
// find phone available size
try {
StatFs stat = new StatFs(Environment.getDataDirectory()
.getPath());
freeSpace = (long) stat.getAvailableBlocks()
* (long) stat.getBlockSize();
if (freeSpace > download_bytes) {
isMemoryAvailable = true;
} else {
isMemoryAvailable = false;
}
} catch (Exception e) {
e.printStackTrace();
isMemoryAvailable = false;
}
}
return isMemoryAvailable;
}
#Override
// Once the image is downloaded, associates it to the imageView
protected void onPostExecute(Bitmap bitmap) {
if (isCancelled()) {
bitmap = null;
}
if (imageViewReference != null) {
ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
}
}
}
static Bitmap downloadBitmap(String url) {
final AndroidHttpClient client = AndroidHttpClient
.newInstance("Android");
final HttpGet getRequest = new HttpGet(url);
try {
HttpResponse response = client.execute(getRequest);
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w("ImageDownloader", "Error " + statusCode
+ " while retrieving bitmap from " + url);
return null;
} else {
Log.w("ImageDownloader", "Success " + statusCode
+ " while retrieving bitmap from " + url);
}
final HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = null;
try {
inputStream = entity.getContent();
final Bitmap bitmap = BitmapFactory
.decodeStream(inputStream);
return bitmap;
} finally {
if (inputStream != null) {
inputStream.close();
}
entity.consumeContent();
}
}
} catch (Exception e) {
// Could provide a more explicit error message for IOException or
// IllegalStateException
getRequest.abort();
Log.w("ImageDownloader", "Error while retrieving bitmap from "
+ url);
} finally {
if (client != null) {
client.close();
}
}
return null;
}
}
Why you download image twice in doInBackground() at the start and the end? You can return the bitmap just downloaded directly.
if your min sdk level >= 11, you can call executeOnExecutor of AsyncTask with param " THREAD_POOL_EXECUTOR" for concurrency.
if your min sdk level < 11, you can implements AsyncTask new API by reference the source code of AsyncTask.
Use a executeOnExecutor
http://developer.android.com/reference/java/util/concurrent/Executor.html
new BitmapDownloaderTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "your urls");
Quoting from docs
http://developer.android.com/reference/android/os/AsyncTask.html
When first introduced, AsyncTasks were executed serially on a single background thread. Starting with DONUT, this was changed to a pool of threads allowing multiple tasks to operate in parallel. Starting with HONEYCOMB, tasks are executed on a single thread to avoid common application errors caused by parallel execution.
If you truly want parallel execution, you can invoke executeOnExecutor(java.util.concurrent.Executor, Object[]) with THREAD_POOL_EXECUTOR.
I previously worked on fetching image from sd card displaying it in a list view, that worked using:
imgView.setImageURI(Uri.parse(ImagePath));
Now, I am trying to display image from URL, with the following lines but the image is not displayed in the list view, the following are the lines used:
imgView.setImageBitmap(getBitmapFromURL(ImagePath));
Where, getBitmapFromURL is:
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
}
catch (IOException e) {
e.printStackTrace();
return null;
}
}
There is no exception displayed, the image just not get displayed.
Need of an urgent solution....
Thanks,
This is a synchronous loading.(Personally I would not use this cause if there are so many Image to be loaded, the apps is a bit laggy)..
URL url = new URL(//your URL);
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bmp);//your imageview
If I were you I would study Async or the lazy adapter..
EDIT
I forgot where I got these code (well thank you for a wonderful code author)
Here it is
public Bitmap getBitmap(String bitmapUrl) {
try {
URL url = new URL(bitmapUrl);
return BitmapFactory.decodeStream(url.openConnection().getInputStream());
}
catch(Exception ex) {return null;}
}
public enum BitmapManager {
INSTANCE;
private final Map<String, SoftReference<Bitmap>> cache;
private final ExecutorService pool;
private Map<ImageView, String> imageViews = Collections
.synchronizedMap(new WeakHashMap<ImageView, String>());
private Bitmap placeholder;
BitmapManager() {
cache = new HashMap<String, SoftReference<Bitmap>>();
pool = Executors.newFixedThreadPool(5);
}
public void setPlaceholder(Bitmap bmp) {
placeholder = bmp;
}
public Bitmap getBitmapFromCache(String url) {
if (cache.containsKey(url)) {
return cache.get(url).get();
}
return null;
}
public void queueJob(final String url, final ImageView imageView,
final int width, final int height) {
/* Create handler in UI thread. */
final Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
String tag = imageViews.get(imageView);
if (tag != null && tag.equals(url)) {
if (msg.obj != null) {
imageView.setImageBitmap((Bitmap) msg.obj);
} else {
imageView.setImageBitmap(placeholder);
Log.d(null, "fail " + url);
}
}
}
};
pool.submit(new Runnable() {
public void run() {
final Bitmap bmp = downloadBitmap(url, width, height);
Message message = Message.obtain();
message.obj = bmp;
Log.d(null, "Item downloaded: " + url);
handler.sendMessage(message);
}
});
}
public void loadBitmap(final String url, final ImageView imageView,
final int width, final int height) {
imageViews.put(imageView, url);
Bitmap bitmap = getBitmapFromCache(url);
// check in UI thread, so no concurrency issues
if (bitmap != null) {
Log.i("inh","Item loaded from cache: " + url);
imageView.setImageBitmap(bitmap);
} else {
imageView.setImageBitmap(placeholder);
queueJob(url, imageView, width, height);
}
}
private Bitmap downloadBitmap(String url, int width, int height) {
try {
Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(
url).getContent());
bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
Log.i("nandi2 ako", ""+bitmap);
cache.put(url, new SoftReference<Bitmap>(bitmap));
return bitmap;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
Now to call it
String fbAvatarUrl = "//Your URL";
BitmapManager.INSTANCE.loadBitmap(fbAvatarUrl, //Your ImageView, 60,60);
//60 60 is my desired height and width
I encountered this kind problem before, you can refer to this thread, if no luck, try my code,
public static Bitmap loadImageFromUrl(String url) {
URL m;
InputStream i = null;
BufferedInputStream bis = null;
ByteArrayOutputStream out =null;
try {
m = new URL(url);
i = (InputStream) m.getContent();
bis = new BufferedInputStream(i,1024 * 8);
out = new ByteArrayOutputStream();
int len=0;
byte[] buffer = new byte[1024];
while((len = bis.read(buffer)) != -1){
out.write(buffer, 0, len);
}
out.close();
bis.close();
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
byte[] data = out.toByteArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
return bitmap;
}
After seeing all the examples from net, i have written the follwoing code to read an image from the URL, show it to the Image view and save it to the path specified.
public class Downloader {
public void startDownload(ImageView i, String path,String url){
BitmapDownloaderTask task = new BitmapDownloaderTask(i,path);
task.execute(url,null,null);
}
static class FlushedInputStream extends FilterInputStream {
public FlushedInputStream(InputStream inputStream) {
super(inputStream);
}
#Override
public long skip(long n) throws IOException {
long totalBytesSkipped = 0L;
while (totalBytesSkipped < n) {
long bytesSkipped = in.skip(n - totalBytesSkipped);
if (bytesSkipped == 0L) {
int b = read();
if (b < 0) {
break; // we reached EOF
} else {
bytesSkipped = 1; // we read one byte
}
}
totalBytesSkipped += bytesSkipped;
}
return totalBytesSkipped;
}
}
static Bitmap downloadBitmap(String url) {
final AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
final HttpGet getRequest = new HttpGet(url);
try {
HttpResponse response = client.execute(getRequest);
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w("ImageDownloader", "Error " + statusCode + " while retrieving bitmap from " + url);
return null;
}
final HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = null;
try {
inputStream = entity.getContent();
final Bitmap bitmap = BitmapFactory.decodeStream(new FlushedInputStream(inputStream));
return bitmap;
} finally {
if (inputStream != null) {
inputStream.close();
}
entity.consumeContent();
}
}
} catch (Exception e) {
// Could provide a more explicit error message for IOException or IllegalStateException
getRequest.abort();
System.err.println("Error while retrieving bitmap from " + url +":"+ e.toString());
} finally {
if (client != null) {
client.close();
}
}
return null;
}
private class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap> {
private String url;
private final WeakReference<ImageView> imageViewReference;
private String path;
public BitmapDownloaderTask(ImageView imageView, String FilePath) {
imageViewReference = new WeakReference<ImageView>(imageView);
path = FilePath;
}
#Override
// Actual download method, run in the task thread
protected Bitmap doInBackground(String... params) {
// params comes from the execute() call: params[0] is the url.
return downloadBitmap(params[0]);
}
#Override
// Once the image is downloaded, associates it to the imageView
protected void onPostExecute(Bitmap bitmap) {
if (isCancelled()) {
bitmap = null;
}
if (imageViewReference != null) {
ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
}
}
OutputStream outStream = null;
File file = new File(path);
try {
outStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
}
catch(Exception e)
{}
}
}
Now the problem is this code works sometimes and fails sometimes... the error is
06-30 12:34:23.363: WARN/System.err(16360): Error while retrieving bitmap from https://URL IS HERE---REMOVE FOR PRIVACY:java.net.SocketTimeoutException: Read timed out
and some time a get
SkImageDecoder::Factory returned null.
Help me with the possible reason
consider this example its show how to create image from url
URL url = new URL(Your Image URL In String);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
yourImageView.setImageBitmap(myBitmap);
you should implement a retry when the connection was failed.