Note that there are some explanatory texts on larger screens.

plurals
  1. PODownloading Image given url faster in android
    primarykey
    data
    text
    <p>I have some url of images, I want to download those and set to my imageView in android. I am using the following code.It is just doing the task. But the process seems to me too slow. I know that it depends on my internet connection speed in my device, but still is there any faster way to speed up loading the image?</p> <pre><code>import java.io.InputStream; import java.lang.ref.WeakReference; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.methods.HttpGet; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.http.AndroidHttpClient; import android.os.AsyncTask; import android.util.Log; import android.widget.ImageView; class ImageDownloaderTask extends AsyncTask&lt;String, Void, Bitmap&gt; { private final WeakReference&lt;ImageView&gt; imageViewReference; public ImageDownloaderTask(ImageView imageView) { imageViewReference = new WeakReference&lt;ImageView&gt;(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. 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) { if (bitmap != null) { imageView.setImageBitmap(bitmap); } else { imageView.setImageDrawable(imageView.getContext().getResources() .getDrawable(R.drawable.imgloading)); } } } } 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(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; } } </code></pre> <p>Waiting for any hint or source code that will just do the same task but faster. Thanx</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload