android - Converting image url to bitmap quickly -


i need display list of images api in list page. used 2 approaches.

first approach:
converting url byte array , converting bitmap.please find below code..

url imageurl = new url(url); urlconnection ucon = imageurl.openconnection();  inputstream = ucon.getinputstream(); bufferedinputstream bis = new bufferedinputstream(is);  bytearraybuffer baf = new bytearraybuffer(500); int current = 0; while ((current = bis.read()) != -1) {      /* approach slowdown process*/      baf.append((byte) current); }  byte[] img_ary= baf.tobytearray(); 

converting byte array bitmap:

bytearrayinputstream imagestream = new bytearrayinputstream(                     imgurl); bitmap theimage = bitmapfactory.decodestream(imagestream); 

second approach:
image scaling based on height , width

private static final string tag_iamge = "image"; private static final int io_buffer_size = 4 * 1024;  public static bitmap loadbitmap(string url) {     bitmap bitmap = null;     inputstream in = null;     bufferedoutputstream out = null;      try {         in = new bufferedinputstream(new url(url).openstream(),                 io_buffer_size);          final bytearrayoutputstream datastream = new bytearrayoutputstream();         out = new bufferedoutputstream(datastream, io_buffer_size);         copy(in, out);         out.flush();          final byte[] data = datastream.tobytearray();         bitmapfactory.options options = new bitmapfactory.options();          options.injustdecodebounds = true;         options.indither = false;         options.inpurgeable = true;         options.ininputshareable = true;          bitmap = bitmapfactory.decodebytearray(data, 0, data.length,                 options);          final int height = options.outheight;         final int width = options.outwidth;         int insamplesize = 1;         int reqheight = 500;         int reqwidth = 500;          if (height > reqheight || width > reqwidth) {              final int halfheight = height / 2;             final int halfwidth = width / 2;              // calculate largest insamplesize value power of 2             // , keeps both             // height , width larger requested height , width.             while ((halfheight / insamplesize) > reqheight                     && (halfwidth / insamplesize) > reqwidth) {                 insamplesize *= 2;             }         }          int scale = insamplesize;          bitmapfactory.options o2 = new bitmapfactory.options();         o2.insamplesize = scale;         o2.indither = false;         o2.inpurgeable = true;         o2.ininputshareable = true;         return bitmapfactory.decodebytearray(data, 0, data.length, o2);     } catch (exception e) {         log.e(tag_iamge, "could not load bitmap from: " + url);     } {         closestream(in);         closestream(out);     }      return bitmap; }  private static void copy(inputstream in, outputstream out)         throws ioexception {     byte[] b = new byte[io_buffer_size];     int read;     while ((read = in.read(b)) != -1) {         out.write(b, 0, read);     } }  private static void closestream(closeable stream) {     if (stream != null) {         try {             stream.close();         } catch (ioexception e) {             android.util.log.e("", "could not close stream", e);         }     } } 

both approaches making app slow. here questions..

  1. how can convert url bitmap without making app slow?
  2. how other apps(like flipcart) displaying 1000>s of images, without slow or hang?

    please guide answer.

there library named picasso. can efficiently load images url. can load image file. wanted , write line of code.

example

picasso.with(context) //context  .load("http://i.imgur.com/dvpvklr.png") //url/file  .into(imageview)//an imageview object show loaded image;  

it can cache image, loaded image able load faster on next time without wasting data.

there many more options available in picasso. here documentation

if need rounded cornered bitmap

    picasso.with(mcontext)             .load("your-image-url-or-file-or-drawable")             .transform(new roundedtransformation(200, 0))             .fit()             .into(imageview); 

roundedtransformation.java

import android.graphics.bitmap; import android.graphics.bitmap.config; import android.graphics.bitmapshader; import android.graphics.canvas; import android.graphics.paint; import android.graphics.rectf; import android.graphics.shader;  // enables hardware accelerated rounded corners // original idea here : http://www.curious-creature.org/2012/12/11/android-recipe-1-image-with-rounded-corners/ public class roundedtransformation implements com.squareup.picasso.transformation {     private final int radius;     private final int margin;  // dp      // radius corner radii in dp     // margin board in dp     public roundedtransformation(final int radius, final int margin) {         this.radius = radius;         this.margin = margin;     }      @override     public bitmap transform(final bitmap source) {         final paint paint = new paint();         paint.setantialias(true);         paint.setshader(new bitmapshader(source, shader.tilemode.clamp, shader.tilemode.clamp));          bitmap output = bitmap.createbitmap(source.getwidth(), source.getheight(), config.argb_8888);         canvas canvas = new canvas(output);         canvas.drawroundrect(new rectf(margin, margin, source.getwidth() - margin, source.getheight() - margin), radius, radius, paint);          if (source != output) {             source.recycle();         }          return output;     }      @override     public string key() {         return "rounded";     } } 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -