android fast load image from url - android

i want load about 50 image into listview
this is my code for load image from url
class ImageDownloader extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public ImageDownloader(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String url = urls[0];
Bitmap mIcon = null;
try {
InputStream in = new java.net.URL(url).openStream();
mIcon = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
}
return mIcon;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
but image load slow.
and I try use Picasso library, but loading speed slow too
how a better solution to load lot of image from url faster.
sorry for my bad English
thank you for reading

You could try to add this to your android manifest
<application android:hardwareAccelerated="true" ...>
i use this to load videos faster, and it works for me ;) Maybe it'll also work for you

you should try UrlImageViewHelper module ,
download the jar from https://github.com/koush/UrlImageViewHelper ,
add it to your android project .
then a simple command
ImageView imgView = (ImageView) findViewById(R.id.someImage);
UrlImageViewHelper.setUrlDrawable(imgView,your uri);
also works with list adapters when views are reused.

Related

Android big image downloading prob : Out of memory

I am trying to download a image file. For some cases the image is so big it is giving OutOfMemoryError in the mid-way. How to deal with this situation?
private class DownloadImage extends AsyncTask<String, Void, Bitmap> {
#Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setTitle("Download Image");
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
#Override
protected Bitmap doInBackground(String... URL) {
String imageURL = URL[0];
Bitmap bitmap = null;
try {
// Download Image from URL
InputStream input = new java.net.URL(imageURL).openStream();
// Decode Bitmap
bitmap = BitmapFactory.decodeStream(input);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
#Override
protected void onPostExecute(Bitmap result) {
// Set the bitmap into ImageView
image.setImageBitmap(result);
// Close progressdialog
mProgressDialog.dismiss();
}
}
First of all you need to determine, in what screen or screen part you need to display it. You need to define result sizes (width and height) of container, that will show your image in application. Then, when you receive your image, you need to scale it down to target size and only then show it.
Here is full stack of tutorials how to do it !link!
But I would recomend you use one of 'load images' libraries. Picasso is a good example. Please check it out here
If you don't want to use libraries like Picasso you can use Volley.
Go search for NetworkImageView , maybe it will hep you.There is one tutorial : using-volley-to-download-cache-and-display-bitmaps.
You can take a look at this question too : how-do-i-properly-set-up-volley-to-download-images-from-a-url
And here too : Volley Request
add "android:largeHeap=true" in application tag of manifest file

Android imageview flickering

In my app I have list view and each item in the list contains an Image view. The image is loaded from bitmap using
imageView.setImageBitmap(bitmap);
For handling concurrency i am using the method described in the following official documentation.
Processing Bitmaps Off the UI Thread
On high density devices the image view flickers. Any possible solution for this problem?
when i use handle concurrency then only the flickering happens and when i am using asynctask only then there is no flickering
Use Picasso , Picasso saves you all the problems with downloading, setting and caching images. The whole code needed for a simple example is:
Just use like this (from UI Thread) :
In listview , use this in getView() , it handles caching and else..
Picasso.with(context)
.load(url)
.into(imageView);
http://square.github.io/picasso/
or
ImageView tre = (ImageView) findViewById(R.id.imageview);
String URL = "http://www...sdsdsd ...";
mChart.setTag(URL);
new DownloadImage.execute(tre);
public class DownloadImage extends AsyncTask<ImageView, Void,Bitmap> {
ImageView imageView = null;
#Override
protected Bitmap doInBackground(ImageView... imageViews) {
this.imageView = imageViews[0];
return download_Image((String)imageView.getTag());
}
#Override
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
}
private Bitmap download_Image(String url) {
Bitmap bmp =null;
try{
URL ulrn = new URL(url);
HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
InputStream is = con.getInputStream();
bmp = BitmapFactory.decodeStream(is);
if (null != bmp)
return bmp;
}catch(Exception e){}
return bmp;
}

canvas: trying to use a recycled bitmap android.graphics.Bitmap on asynctask

I'm getting this error when trying to retrieve an image from a url using Asynctask.
This is my asynctask:
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap result = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
result = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return result;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
if (result != null && !result.isRecycled()) {
result.recycle();
result = null;
}
}
}
If I remove the result.recycle(), the error would be OutofMemoryError.
I'm retrieving multiple images from different url.
How can I do this?
I'm calling the asynctask by:
new DownloadImageTask(imageview[i]).execute(paths.get(i));
Many thanks, :)
You can't recycle here because you still need the bitmap. The problem is that the total amount of memory used by these bitmaps is greater than the total amount of memory available (when added to the memory used by the app already). And because of that, the suggestion 1 person gave to use a library won't help- it won't add memory. Here's a few things you can do:
1)Reduce the memory used by your app. May or may not be possible, look at a heap profiler and see if you have memory leaks.
2)Do not download all the images at once.
3)Rather than downloading them into memory, write it to a file and open each image only when actually needed. This can be combined with an LRUCache to make sure you never use more than a fixed amount of memory.
Use picasa library
ImageView product_image = (ImageView) itemview
.findViewById(R.id.product_image);
Picasso.with(context)
.load(product_order_details.get(position).product_image)
.placeholder(R.drawable.defalutimage).fit().into(product_image);

Android - Load image from URL not working

please help me with these code, It doesn't show image from URL.
And another question, how to debug each functions such as "url.openConnection", "getInputStream".... to confirm they work well or not?
Thank you
public class MainActivity extends Activity {
Bitmap bitmap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout mainLayout = (LinearLayout)findViewById(R.id.mainlayout);
// ImageView
ImageView imgView = new ImageView(this);
mainLayout.addView(imgView);
bitmap = loadImage("https://www.google.com.vn/logos/doodles/2014/dian-fosseys-82nd-birthday-5702250374627328-hp.jpg");
imgView.setImageBitmap(bitmap);
}
public Bitmap loadImage(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);
input.close();
return myBitmap;
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}
}
The problem is that you are doing network communications on the main thread, and assuredly getting a NetworkOnMainThreadException because of it.
While you could implement an AsyncTask to manage your network calls, I would strongly suggest using a networking-capable image library to do all the grunt work for you.
https://github.com/nostra13/Android-Universal-Image-Loader
Probably a good idea to choose ImageLoader library rather than doing all this nitty gritty stuff yourself. It is not optimised and error prone too.

Show image from URL, Android-app goes extremly slow

I am trying to show an image in my app from an URL, but when (only when) the bitmap is showed the app gets way too slow. The app goes OK, but when I load the image it goes extremly slow. Someone can help with this?
So I have a class named ImageDownload which does the download and in my Fragment I have:
new ImageDownload((ImageView) login_image, this.getActivity().getApplicationContext(),"login_image")
.execute(path);
Here is my download Class:
public class ImageDownload extends AsyncTask<String, Void, Bitmap> {
private Context mContext;
private String filename;
ImageView bmImage;
public ImageDownload(ImageView bmImage, Context mContext, String filename) {
this.bmImage = bmImage;
this.mContext = mContext;
this.filename = filename;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in,null,null);
//EDIT:
in.close();
// The isuue is not soved yet
} catch (Exception e) {
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
Then the problem with the size of your ImageBitmap & the RAM size of the tablet. It is taking too much of RAM on loading the ImageBitmap on RAM. You can detect this by looking into the log, how frequent System.gc() is getting called, as the system facing too frequent page misss than page hit, its almost like thrashing problem. I'm sure the system is calling .gc() so frequently and as the System.gc() is called in GUI thread, it is disturbing UI experience.
You should be using Google's Volley library
Use Picasso. It's simple and cool.
I finally found the response. Check it out. it might help you in the future. Thank you to all of you. This works perfectly:
http://www.technotalkative.com/android-load-images-from-web-and-caching/

Categories

Resources