Android: Gallery Widget performance is terrible, any reasons? - android

I have an Android Gallery widget that displays several ImageViews with pictures from the web. The images are stored as jpgs, and get converted to bitmap before added to the ImageViews. Each jpg is 8kb. I'm not doing any scaling on the images.
When I use the gallery with 1 or 2 pictures, it works fine, scrolling is smooth, etc. With 3, it starts to get a little choppy, and at 5 or 10 pictures the application is pretty much unusable.
Does anyone have any ideas as to why the performance is so bad? Does anyone have suggestions for alternatives? Thank you-
#elevine: my method to construct bitmap from jpg url:
private Bitmap getBitmapFromURL(String input) throws IOException {
URL url = new URL(input);
URLConnection conn = url.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
Bitmap bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
return bm;
}
This is my getView method from my ImageAdapter. I'm beginning to suspect this is where my problem lies... Am I grabbing the images way too many times? Thanks for your help!
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
Bitmap bm;
try {
imageView.setImageBitmap(getBitmapFromURL(urls.get(position)));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//mageView.
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new Gallery.LayoutParams(100,100));
return imageView;
}

If you are calling getBitmapFromURL from your Activity, then it might be blocking the UI thread. Make sure this code runs on a separate Thread or inside an AsyncTask. You may also want to cache the Bitmaps inside a WeakHashmap. Check for the image inside the cache before grabbing it from the network.

Here are some tip itthat might come handy:
Try to cache the image first on memory with WeakReference and also on disk (if its possible) so you don't have to waste mobile resources on downloading the image all over again.
Performance could be bad if you override the GalleryAdapter and you are not helping the adapter to recycle your views List items
Also try to execute download operation on a different Thread, consider using AsyncTask.
Here is an interesting ImageManager you might take use

One other item to consider as well is the animationDuration. You can set this to 0 and it will help with the scrolling performance. I needed to do this recently with a Google TV app I'm working on otherwise there was a second or two pause. Pre-caching your images is also recommended and reducing the number of images that are displayed at a time if possible.

Related

Android select an image from the gallery and resize it before I pull it in

I'm running into issues with the images in my gallery being much much larger than I need them to be.
I've looked for ways to reduce their size before I actually pull them in, but I'm just not quite putting it together as most of what I'm finding deals with BitMap resources and not a BitMap that already exists in the gallery.
So, basically, I am getting the image like so,
imageBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
But before I actually assign it to my BitMap var over there I need to scale it down to a reasonable size for a phone.
Any help with understanding what is going on here better is appreciated.
getBitmap() is a weak convenience method. Its body is a whopping four lines of code:
public static final Bitmap getBitmap(ContentResolver cr, Uri url)
throws FileNotFoundException, IOException {
InputStream input = cr.openInputStream(url);
Bitmap bitmap = BitmapFactory.decodeStream(input);
input.close();
return bitmap;
}
This is why I don't bother teaching people about it.
IMHO, the best solution is for you to use one of the many image-loading libraries available for Android, such as Picasso. Most of the good ones can load from a Uri and handle your resizing as part of the operation, doing the heavy lifting on a background thread.
If, for whatever reason, you want to do all that work yourself, call BitmapFactory.decodeStream() with a BitmapFactory.Options object. In particular, set inSampleSize to indicate that you want the image to be resampled as part of reading it, so you wind up with a smaller Bitmap taking up less heap space.

Using uri (android.net.uri) as a link to get image from internet and put it in a ImageView

I have a item object, containing a Uri variable called image. something like this
http://icons.iconarchive.com/icons/designcontest/vintage/256/Type-icon.png
I tried to get from the class method the Uri and use it for setting up the ImageView called Image in my viewholder
viewHolder.Image.setImageURI(item.getImage());
but it didn't work, so I tried something more complex without success
//Image
URL url = null;
try {
url = new URL(item.getImage().toString());
} catch (MalformedURLException e) {
e.printStackTrace();
}
Bitmap bmp = null;
try {
bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
viewHolder.Image.setImageBitmap(bmp);
} catch (IOException e) {
e.printStackTrace();
}
I have allowed internet permissions, and if I use some images from local with Uri.parse it works without any problem.
Here's the error I get from logcat
E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'java.net.URLConnection java.net.URL.openConnection()' on a null object reference
I think the problem is something in the url definiton but I'll ask you if someone had my same problem and how you solved. Thanks in advance!
for this purpose try to use a specific image library like Picasso, Glide or Fresco. The first one is the simplest with nice fluent interface and it loads images pretty fast:
Please check it, as you may don't know it. Here is a short description taken from its official site (link below):
Images add much-needed context and visual flair to Android
applications. Picasso allows for hassle-free image loading in your
application—often in one line of code!
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
Many common pitfalls of image loading on Android are handled
automatically by Picasso:
Handling ImageView recycling and download cancelation in an adapter.
Complex image transformations with minimal memory use.
Automatic memory and disk caching.
From: http://square.github.io/picasso/
As it already Open Source project you can check the code to improve it or make your own version fitted to your actual needs.
Hope it help
In all likelihood, url = new URL(item.getImage().toString()); is throwing a MalformedURLException, which your catch block is suppressing. As a result, url is null, which is why you're getting a NullPointerException. A constructor can't return a null, so this is the only thing I can imagine.
Unless you really know what you're doing, I recommend you use a library like Glide to do image fetching.

How to load thumbnails of Pic Clicked via Camera Intent

My application takes pictures via camera intent. How should I display their small size version in a grid view for viewing purpose. Should I create their thumbnails and store them in cache or external storage Or should I use the thumbnails created by Default Gallery application. My pictures are stored in external storage so I am expecting that Default Gallery Application would make their thumbnails automatically. If yes, then how should I map each image with the thumbnail created by Default Gallery Application.
Well, I have found that Async class could handle the memory usage scenario.
The relevant link is: http://developer.android.com/intl/es/reference/android/os/AsyncTask.html
Well, I have got an answer
public Bitmap getbitpam(String path) {
Bitmap imgthumBitmap = null;
try {
final int THUMBNAIL_SIZE =300 ;
FileInputStream fis = new FileInputStream(path);
imgthumBitmap = BitmapFactory.decodeStream(fis);
imgthumBitmap = Bitmap.createScaledBitmap(imgthumBitmap,
THUMBNAIL_SIZE, THUMBNAIL_SIZE, false);
ByteArrayOutputStream bytearroutstream = new ByteArrayOutputStream();
imgthumBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytearroutstream);
} catch (Exception ex) {
}
return imgthumBitmap;
}
However, this is taking a lot of RAM. I Have also found a strange behavior. That as I am scrolling in grid view, it is taking more RAM. The growth in memory used is cumulative and finally the app is crashing due to Memory_Low exception. Any workaround for it??
Got answer for second problem too:-- Async class.

Setting up remote bitmap faster in UI(ImageView)

I am writing an app that is essentially a flipcard that shows word/hint on one side and picture on other side relevant to it.I am using viewflipper for the two views.Problem is that the picture loads from internet.App access the db,extracts url and then loads picture.That means the change in view takes as much time as it takes to download the picture.I want to flip card immediately and load picture so that user do not thinks that app is slow.Rather they should know that picture is being loaded,hence the delay.Pls suggest improvement in code.My code for loading picture in flipcard is:
public void setBMP(String s) //String passed is url extracted from column of db uing
{ //internal db
try{
//String url1 = "c.getString(3)";
String url1= s;
System.out.println(url1);
URL ulrn = new URL(url1);
HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
InputStream is = con.getInputStream();
Bitmap bmp = BitmapFactory.decodeStream(is);
if (null != bmp)
{
im.setImageBitmap(bmp);
}
else
System.out.println("The Bitmap is NULL");
}catch(Exception e){}
}
}
For changing view, i have set up actionListener.As soon as user touches screen card flips and image loads.
Also is it possible to preload the images in background while user is viewing some other card.Or is it possible to cache the cards viewed?
I would go the asnyctask route, because that way you can load/disable spinners (or wahtever loading animations) as well. Check out this answer for a really simple example. If you want to add spinners you need to start them in the onPreExecute() of the asnyctask (just add it to the example) and disable them in onPostExecute after you image is downloaded.
Using AsyncTask to load Images in ListView
it seems to me that creating a Runnable that gets the bmp and saves it to a hash map file and then, when it's needed if downloaded, it opens the file, and if not it downloads it from the web.
try looking at fedorvlasov's Lazy adapter for reference.
you can use his Image loader

how to load internet images in gridview efficiently?

I am using following example to display internet images in my activity.
http://developer.android.com/resources/tutorials/views/hello-gridview.html
In custom image adapter I'm directly loading images from internet and assigning it to imageview.
Which shows images in gridview and every thing works fine but it is not efficient way.
When ever i scroll gridview it again and again loads images and thats why gridview scrolls very slow
Is there caching or some useful technique available to make it faster?
Create a global and static method which returns a Bitmap. This method will take parameters: context,imageUrl, and imageName.
in the method:
check if the file already exists in the cache. if it does, return the bitmap
if(new File(context.getCacheDir(), imageName).exists())
return BitmapFactory.decodeFile(new File(context.getCacheDir(), imageName).getPath());
otherwise you must load the image from the web, and save it to the cache:
image = BitmapFactory.decodeStream(HttpClient.fetchInputStream(imageUrl));
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File(context.getCacheDir(), imageName));
}
//this should never happen
catch(FileNotFoundException e) {
if(Constants.LOGGING)
Log.e(TAG, e.toString(), e);
}
//if the file couldn't be saved
if(!image.compress(Bitmap.CompressFormat.JPEG, 100, fos)) {
Log.e(TAG, "The image could not be saved: " + imageName + " - " + imageUrl);
image = BitmapFactory.decodeResource(context.getResources(), R.drawable.default_cached_image);
}
fos.flush();
fos.close();
return image;
preload a Vector<SoftReference<Bitmap>> object with all of the bitmaps using the method above in an AsyncTask class, and also another List holding a Map of imageUrls and imageNames(for later access when you need to reload an image), then set your GridView adapter.
i recommend using an array of SoftReferences to reduce the amount of memory used. if you have a huge array of bitmaps you're likely to run into memory problems.
so in your getView method, you may have something like(where icons is a Vector holding type SoftReference<Bitmap>:
myImageView.setImageBitmap(icons.get(position).get());
you would need to do a check:
if(icons.get(position).get() == null) {
myImageView.setImageBitmap(defaultBitmap);
new ReloadImageTask(context).execute(position);
}
in the ReloadImageTask AsyncTask class, simply call the global method created from above with the correct params, then notifyDataSetChanged in onPostExecute
some additional work may need to be done to ensure you don't start this AsyncTask when it is already running for a particular item
You will need to implement the caching yourself. Create a proxy class that will download the images. In the getView ask this class to download an image by passing a url. In the proxy class create a HashMap that will map a url to a Bitmap. If the key for the passed url doesn't exist, download the image and store it. Otherwise returned the stored bitmap converted to an imageView.
Of course you can't afford to store as many images as you like. You need to set a limit, for example 10 images, based on the image size you expect to have. When the limit is exceeded, you need to discard old images in the favor of new ones.
You could try DroidFu. My app uses the ImageCache. There's also some manner of web-based imageview or something of the sort in the library. See in particular WebImageView and WebGalleryAdapter: http://mttkay.github.com/droid-fu/index-all.html
Edited to add: The droid-fu project is deprecated in favor of Ignition. https://github.com/mttkay/ignition

Categories

Resources