Screenshot
Gridview Example
I have been trying to display images from url in this GirdView. Some images appear fine, but some images are not appearing correctly. I am also saving the images in cache for making it show images faster next time. This is my code.
MainActivity.java
package com.example.gridview;
public class MainActivity extends Activity {
ArrayList<String> imageList = new ArrayList<String>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageList
.add("http://wallpapersus.com/wallpapers/2012/10/Megan-Fox-22-854x960.jpg");
imageList.add("http://www.wallpapersdesign.net
/wallpapers/2013/12/Transformers-Megan-Fox-854x960.jpg");
imageList
.add("http://wallpapersus.com/wallpapers/2012/10/Megan-Fox1-854x960.jpg");
imageList
.add("http://wallpapersus.com/wallpapers/2012/10/Megan-Fox-24-854x960.jpg");
imageList
.add("http://topwalls.net/wallpapers/2013/06/Wallpapers-Megan-Fox-11-854x960.jpg");
imageList
.add("http://topwalls.net/wallpapers/2013/06/Megan-Fox-133-854x960.jpg");
imageList
.add("http://topwalls.net/wallpapers/2013/06/Megan-Fox-89-854x960.jpg");
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this, imageList));
gridview.setOnItemClickListener(new OnImageClickListener(1));
}
class OnImageClickListener implements OnItemClickListener {
public int position;
// constructor
public OnImageClickListener(int position) {
this.position = position;
}
#Override
public void onItemClick(AdapterView<?> v, View arg1, int arg2, long arg3) {
Intent i = new Intent(getApplicationContext(),
FullScreenViewActivity.class);
Bundle basket = new Bundle();
basket.putStringArrayList("url", imageList);
basket.putString("position", String.valueOf(arg2));
i.putExtras(basket);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(i);
Log.e("Activity ImageClass", "Left from this class");
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_settings:
refresh();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void refresh() {
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this, imageList));
gridview.setOnItemClickListener(new OnImageClickListener(1));
}
}
This is the adapter
package com.example.gridview;
public class ImageAdapter extends BaseAdapter {
private Activity mContext;
ArrayList<String> imageList;
ImageLoader imageLoader;
ProgressDialog progress;
int versionCode = 10;
public ImageAdapter(Activity c, ArrayList<String> imageList) {
mContext = c;
this.imageList = imageList;
imageLoader = new ImageLoader(mContext, 200);
}
#Override
public int getCount() {
return imageList.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some
// attributes
imageView = new ImageView(mContext);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
Log.e("Inside", "if");
imageView.setLayoutParams(new GridView.LayoutParams(185, 185));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
imageView.setId(position);
} else {
Log.e("Inside", "else");
imageView.setLayoutParams(new GridView.LayoutParams(90, 90));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
}
} else {
imageView = (ImageView) convertView;
}
//imageLoader.clearCache();
imageLoader.DisplayImage(imageList.get(position), imageView);
return imageView;
}
}
ImageLoader.java
package com.example.gridview;
public class ImageLoader {
MemoryCache memoryCache = new MemoryCache();
FileCache fileCache;
public int REQUIRED_SIZE;
private Map<ImageView, String> imageViews = Collections
.synchronizedMap(new WeakHashMap<ImageView, String>());
ExecutorService executorService;
Activity context;
public ImageLoader(Activity context, int size) {
fileCache = new FileCache(context, size);
this.context = context;
executorService = Executors.newFixedThreadPool(20);
REQUIRED_SIZE = size;
}
final int stub_id = R.drawable.ic_menu_camera;
public void DisplayImage(String url, ImageView imageView) {
imageViews.put(imageView, url);
Bitmap bitmap = memoryCache.get(url);
if (bitmap != null)
imageView.setImageBitmap(bitmap);
else {
queuePhoto(url, imageView);
imageView.setImageResource(stub_id);
}
}
public void setWallpaper(String url) {
Bitmap bitmap = memoryCache.get(url);
if (bitmap != null) {
WallpaperManager myWallpaperManager = WallpaperManager
.getInstance(context);
// below line of code will set any image which is in the
// drawable
// folder
// myWallpaperManager.setResource(R.drawable.icon);
DisplayMetrics metrics = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
Bitmap bitmap1 = Bitmap.createScaledBitmap(bitmap, width, height,
true);
WallpaperManager wallpaperManager = WallpaperManager
.getInstance(context);
// wallpaperManager.setWallpaperOffsetSteps(1, 1);
wallpaperManager.suggestDesiredDimensions(width, height);
try {
wallpaperManager.setBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void DisplayImageCircle(String url, ImageView imageView) {
imageViews.put(imageView, url);
Bitmap bitmap = memoryCache.get(url);
if (bitmap != null)
imageView.setImageBitmap(bitmap);
else {
queuePhoto(url, imageView);
imageView.setImageResource(stub_id);
}
}
private void queuePhoto(String url, ImageView imageView) {
PhotoToLoad p = new PhotoToLoad(url, imageView);
executorService.submit(new PhotosLoader(p));
}
private Bitmap getBitmap(String url) {
File f = fileCache.getFile(url);
// from SD cache
Bitmap b = decodeFile(f);
if (b != null)
return b;
// from web
try {
Bitmap bitmap = null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) imageUrl
.openConnection();
conn.setConnectTimeout(6000000);
conn.setReadTimeout(6000000);
conn.setInstanceFollowRedirects(true);
InputStream is = conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
bitmap = decodeFile(f);
return bitmap;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
// decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f) {
try {
// decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
}
return null;
}
// Task for the queue
private class PhotoToLoad {
public String url;
public ImageView imageView;
public PhotoToLoad(String u, ImageView i) {
url = u;
imageView = i;
}
}
class PhotosLoader implements Runnable {
PhotoToLoad photoToLoad;
PhotosLoader(PhotoToLoad photoToLoad) {
this.photoToLoad = photoToLoad;
}
#Override
public void run() {
if (imageViewReused(photoToLoad))
return;
Bitmap bmp = getBitmap(photoToLoad.url);
memoryCache.put(photoToLoad.url, bmp);
if (imageViewReused(photoToLoad))
return;
BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad);
Activity a = (Activity) photoToLoad.imageView.getContext();
a.runOnUiThread(bd);
}
}
boolean imageViewReused(PhotoToLoad photoToLoad) {
String tag = imageViews.get(photoToLoad.imageView);
if (tag == null || !tag.equals(photoToLoad.url))
return true;
return false;
}
// Used to display bitmap in the UI thread
class BitmapDisplayer implements Runnable {
Bitmap bitmap;
PhotoToLoad photoToLoad;
public BitmapDisplayer(Bitmap b, PhotoToLoad p) {
bitmap = b;
photoToLoad = p;
}
#Override
public void run() {
if (imageViewReused(photoToLoad))
return;
if (bitmap != null)
photoToLoad.imageView.setImageBitmap(bitmap);
else
photoToLoad.imageView.setImageResource(stub_id);
}
}
public void clearCache() {
memoryCache.clear();
fileCache.clear();
}
}
I also tried running imageLoader in doInBackground() but it was of no use.
Please Help !!
set your url also in ImageView
imageView.setTag(yourURL);
imageLoader.DisplayImage(imageList.get(position), imageView);
Related
I'm currently developing an app where I have a listview. This listview is populated with text and images fetched from a XML file online. It's populating correctly however, whenever you scroll, anything that wasn't currently on the screen reloads and recalculates the size of the bitmap to go in the listview. This leads to the scrolling being less fluid and annoying. How can I get my activity to populate the listview with the images and load them one time to stay there?
ImageLoader:
public class ImageLoader {
MemoryCache memoryCache = new MemoryCache();
FileCache fileCache;
private Map<ImageView, String> imageViews = Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
ExecutorService executorService;
int size;
int placeholderpic;
public ImageLoader(Context context, int size, int placeholderpic) {
fileCache = new FileCache(context);
executorService = Executors.newFixedThreadPool(5);
this.size = size;
this.placeholderpic = placeholderpic;
}
public void displayImage(String url, ImageView imageView) {
imageViews.put(imageView, url);
Bitmap bitmap = memoryCache.get(url);
if(bitmap != null)
imageView.setImageBitmap(bitmap);
else {
queuePhoto(url, imageView);
imageView.setImageResource(placeholderpic);
}
}
private void queuePhoto(String url, ImageView imageView) {
PhotoToLoad p = new PhotoToLoad(url, imageView);
executorService.submit(new PhotosLoader(p));
}
private Bitmap getBitmap(String url) {
File f = fileCache.getFile(url);
//from SD cache
Bitmap b = decodeFile(f);
if(b != null)
return b;
//from web
try {
Bitmap bitmap = null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is=conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
bitmap = decodeFile(f);
return bitmap;
} catch (Throwable ex) {
ex.printStackTrace();
if(ex instanceof OutOfMemoryError)
memoryCache.clear();
return null;
}
}
//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f) {
try {
//decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = size;
int width_tmp = o.outWidth, height_tmp=o.outHeight;
int scale = 1;
while(true){
if(width_tmp/2 < REQUIRED_SIZE || height_tmp/2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
//decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}
//Task for the queue
private class PhotoToLoad {
public String url;
public ImageView imageView;
public PhotoToLoad(String u, ImageView i) {
url = u;
imageView = i;
}
}
class PhotosLoader implements Runnable {
PhotoToLoad photoToLoad;
PhotosLoader(PhotoToLoad photoToLoad){
this.photoToLoad = photoToLoad;
}
#Override
public void run() {
if(imageViewReused(photoToLoad))
return;
Bitmap bmp = getBitmap(photoToLoad.url);
memoryCache.put(photoToLoad.url, bmp);
if(imageViewReused(photoToLoad))
return;
BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad);
Activity a = (Activity)photoToLoad.imageView.getContext();
a.runOnUiThread(bd);
}
}
boolean imageViewReused(PhotoToLoad photoToLoad) {
String tag=imageViews.get(photoToLoad.imageView);
if(tag == null || !tag.equals(photoToLoad.url))
return true;
return false;
}
//Used to display bitmap in the UI thread
class BitmapDisplayer implements Runnable {
Bitmap bitmap;
PhotoToLoad photoToLoad;
public BitmapDisplayer(Bitmap b, PhotoToLoad p){
bitmap = b; photoToLoad = p;
}
public void run() {
if(imageViewReused(photoToLoad))
return;
if(bitmap != null)
photoToLoad.imageView.setImageBitmap(bitmap);
else
photoToLoad.imageView.setImageResource(placeholderpic);
}
}
public void clearCache() {
memoryCache.clear();
fileCache.clear();
}
ListView Adapter:
public class LazyNewsAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<News> listData;
private LayoutInflater inflater = null;
public LazyNewsAdapter(Activity activity, ArrayList<News> listData) {
this.activity = activity;
this.listData = listData;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return listData.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
News newsItem = listData.get(position);
View view = convertView;
if(convertView == null)
view = inflater.inflate(R.layout.news_cell, null);
TextView newsTitle = (TextView) view.findViewById(R.id.newsTitle);
TextView newsDate = (TextView) view.findViewById(R.id.newsDate);
ImageView image = (ImageView) view.findViewById(R.id.newsImage);
newsTitle.setText(newsItem.getNewsTitle());
newsDate.setText(newsItem.getNewsDate());
String url = newsItem.getNewsImageUrl();
ImageLoader imageLoader = new ImageLoader(activity, 600, R.drawable.placeholder);
imageLoader.displayImage(url, image);
return view;
}
You must use cache for that kind of things.
See Universal Image loader to load your images in an efficient way.
You must have only one instance of your imageLoader in an activity, dont instantiate it in each getView:
Do that line in your onCreate instead of getView:
ImageLoader imageLoader = new ImageLoader(activity, 600, R.drawable.placeholder);
Try to save online data to Array List variable. Then load it to list view with using notifydatasetChange
Use the listview optimization for the setting image in ListAdapter here's video tutorial that might help you also be sure to use Glide for setting the image as they take a step forward of recycling your Bitmap
https://www.youtube.com/watch?v=mKGoKnhN_Ys&index=91&list=PL1q3ROAofjeOUwh7lPBnGbg__DUodwLN7
below is my imageloader code which work fine if new image are diffrent name but if new image are same name then is not change images show old image only suppose i have image name myimage is display perfectly if i add new image in url with same name and delete previous my code again show previous image not show new image due to cache what is problem in this code help me please
private ImageLoader imgLoader;
private String strUrl = "http://solutionboat.com/work_2/asset/images/rifat.jpg";
private String strUrl1 = "http://solutionboat.com/work_2/asset/images/sobin.jpg";
private String strUrl2 = "http://solutionboat.com/work_2/asset/images/samol.jpg";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView target0 = (ImageView) findViewById(R.id.target0);
ImageView target1 = (ImageView) findViewById(R.id.target1);
ImageView target2 = (ImageView) findViewById(R.id.target2);
imgLoader = new ImageLoader(this);
imgLoader.displayImage(strUrl, target0);
imgLoader.displayImage(strUrl1, target1);
imgLoader.displayImage(strUrl2, target2);
ViewFlipper flipper = (ViewFlipper) findViewById(R.id.viewFlipper1);
flipper.startFlipping();
}
}
ImageLoader class
public class ImageLoader {
MemoryCache memoryCache = new MemoryCache();
FileCache fileCache;
private Map<ImageView, String> imageViews =
Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
ExecutorService executorService;
public ImageLoader(Context context) {
fileCache = new FileCache(context);
executorService = Executors.newFixedThreadPool(5);
}
final int stub_id = R.mipmap.ic_launcher;
public void displayImage(String url, ImageView imageView) {
imageViews.put(imageView, url);
Bitmap bitmap = memoryCache.get(url);
if(bitmap != null) {
imageView.setImageBitmap(bitmap);
} else {
queuePhoto(url, imageView);
imageView.setImageResource(stub_id);
}
}
private void queuePhoto(String url, ImageView imageView) {
PhotoToLoad p = new PhotoToLoad(url, imageView);
executorService.submit(new PhotosLoader(p));
}
private Bitmap getBitmap(String url) {
File f = fileCache.getFile(url);
Bitmap b = decodeFile(f);
if(b != null)
return b;
try {
Bitmap bitmap = null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is = conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.copyStream(is, os);
os.close();
bitmap = decodeFile(f);
return bitmap;
} catch (Throwable ex) {
ex.printStackTrace();
return null;
}
}
private Bitmap decodeFile(File f) {
try {
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
final int REQUIRED_SIZE = 70;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while(true) {
if(width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
break;
}
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
private class PhotoToLoad{
public String url;
public ImageView imageView;
public PhotoToLoad(String u, ImageView i) {
url = u;
imageView = i;
}
}
class PhotosLoader implements Runnable {
PhotoToLoad photoToLoad;
PhotosLoader(PhotoToLoad photoToLoad) {
this.photoToLoad = photoToLoad;
}
#Override
public void run() {
if(imageViewReused(photoToLoad)) {
return;
}
Bitmap bmp = getBitmap(photoToLoad.url);
memoryCache.put(photoToLoad.url, bmp);
if(imageViewReused(photoToLoad)) {
return;
}
BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad);
Activity a = (Activity) photoToLoad.imageView.getContext();
a.runOnUiThread(bd);
}
}
boolean imageViewReused(PhotoToLoad photoToLoad) {
String tag = imageViews.get(photoToLoad.imageView);
if(tag == null || !tag.equals(photoToLoad.url)) {
return true;
}
return false;
}
class BitmapDisplayer implements Runnable {
Bitmap bitmap;
PhotoToLoad photoToLoad;
public BitmapDisplayer(Bitmap b, PhotoToLoad p) {
bitmap = b;
photoToLoad = p;
}
#Override
public void run() {
if(imageViewReused(photoToLoad))
return;
if(bitmap != null){
photoToLoad.imageView.setImageBitmap(bitmap);
} else {
photoToLoad.imageView.setImageResource(stub_id);
}
}
}
public void clearCahce() {
memoryCache.clear();
fileCache.clear();
}
}
Use Picasso for any image loading/caching problems.
http://square.github.io/picasso/
I have a customized list view in my application, which is showing an image and text (Json). The image I am getting from URL is working, but listview is laggy (scrolling lag).
Maybe my problem is ImageLoader.java class
This is my code:
public class ImageLoader {
MemoryCache memoryCache = new MemoryCache();
FileCache fileCache;
private Map<ImageView, String> imageViews = Collections
.synchronizedMap(new WeakHashMap<ImageView, String>());
ExecutorService executorService;
public ImageLoader(Context context) {
fileCache = new FileCache(context);
executorService = Executors.newFixedThreadPool(5);
}
final int stub_id = R.drawable.aaaaaaaaaaaaaaa;
public void DisplayImage(String url, ImageView imageView) {
imageViews.put(imageView, url);
Bitmap bitmap = memoryCache.get(url);
if (bitmap != null)
imageView.setImageBitmap(bitmap);
else {
queuePhoto(url, imageView);
imageView.setImageResource(stub_id);
}
}
private void queuePhoto(String url, ImageView imageView) {
PhotoToLoad p = new PhotoToLoad(url, imageView);
executorService.submit(new PhotosLoader(p));
}
private Bitmap getBitmap(String url) {
File f = fileCache.getFile(url);
// from SD cache
Bitmap b = decodeFile(f);
if (b != null)
return b;
// from web
try {
Bitmap bitmap = null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) imageUrl
.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is = conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
bitmap = decodeFile(f);
return bitmap;
} catch (Throwable ex) {
ex.printStackTrace();
if (ex instanceof OutOfMemoryError)
memoryCache.clear();
return null;
}
}
// decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f) {
try {
// decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
FileInputStream stream1 = new FileInputStream(f);
BitmapFactory.decodeStream(stream1, null, o);
stream1.close();
// Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = 70;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
FileInputStream stream2 = new FileInputStream(f);
Bitmap bitmap = BitmapFactory.decodeStream(stream2, null, o2);
stream2.close();
return bitmap;
} catch (FileNotFoundException e) {
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
// Task for the queue
private class PhotoToLoad {
public String url;
public ImageView imageView;
public PhotoToLoad(String u, ImageView i) {
url = u;
imageView = i;
}
}
class PhotosLoader implements Runnable {
PhotoToLoad photoToLoad;
PhotosLoader(PhotoToLoad photoToLoad) {
this.photoToLoad = photoToLoad;
}
#Override
public void run() {
try {
if (imageViewReused(photoToLoad))
return;
Bitmap bmp = getBitmap(photoToLoad.url);
memoryCache.put(photoToLoad.url, bmp);
if (imageViewReused(photoToLoad))
return;
BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad);
Activity a = (Activity) photoToLoad.imageView.getContext();
a.runOnUiThread(bd);
} catch (Throwable th) {
th.printStackTrace();
}
}
}
boolean imageViewReused(PhotoToLoad photoToLoad) {
String tag = imageViews.get(photoToLoad.imageView);
if (tag == null || !tag.equals(photoToLoad.url))
return true;
return false;
}
// Used to display bitmap in the UI thread
class BitmapDisplayer implements Runnable {
Bitmap bitmap;
PhotoToLoad photoToLoad;
public BitmapDisplayer(Bitmap b, PhotoToLoad p) {
bitmap = b;
photoToLoad = p;
}
public void run() {
if (imageViewReused(photoToLoad))
return;
if (bitmap != null)
photoToLoad.imageView.setImageBitmap(bitmap);
else
photoToLoad.imageView.setImageResource(stub_id);
}
}
public void clearCache() {
memoryCache.clear();
fileCache.clear();
}
}
This is a also my BaseAdapter.java class
public class BRIgeAdapter extends BaseAdapter {
private LayoutInflater inflater;
private Activity activity;
public ArrayList<HashMap<String, String>> data;
public ViewHolder holder;
public ImageLoader imageLoader;
HashMap<String, String> itemList;
private int screenSize;
MainActivity main;
public BRIgeAdapter(Activity a, ArrayList<HashMap<String, String>> d,int screenSize) {
this.activity = a;
this.data = d;
this.screenSize = screenSize;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return data.get(position);
}
public long getItemId(int position) {
return (long) position;
}
#Override
public int getViewTypeCount() {
return data.size();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder = new ViewHolder();
main = new MainActivity();
convertView = inflater.inflate(R.layout.list_row, parent, false);
holder.journal = (TextView) convertView
.findViewById(R.id.smalljournal);
holder.statId = (TextView) convertView
.findViewById(R.id.smallstatID);
holder.smallDescription1 = (TextView) convertView
.findViewById(R.id.smallDescription1);
holder.DateTime = (TextView) convertView
.findViewById(R.id.smallDateTime);
holder.thumb_image = (ImageView) convertView
.findViewById(R.id.smallthumb);
holder.title = (TextView) convertView.findViewById(R.id.smalltitle);
holder.description = (TextView) convertView
.findViewById(R.id.smallDescription);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
itemList = data.get(position);
if (itemList != null) {
holder.journal.setText(itemList.get(MainActivity.KEY_journal));
// holder.DateTime.setText(itemList.get(MainActivity.KEY_pubDate));
holder.statId.setText(itemList.get(MainActivity.KEY_statID));
holder.smallDescription1.setText(itemList.get(MainActivity.KEY_description));
holder.journal.setTypeface(MainActivity.tf2);
String titleString = itemList.get(MainActivity.KEY_title);
Calendar cal = new GregorianCalendar(2013, 11, 20);
DateFormat df = new SimpleDateFormat("dd.MM.yyyy");
// DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String date = df.format(cal.getTime());
String DateTime = itemList.get(MainActivity.KEY_pubDate);
DateTime = date;
holder.DateTime.setText(DateTime);
holder.title.setText(titleString);
holder.title.setTypeface(MainActivity.tf2);
holder.title.setLineSpacing(2, 1.2f);
holder.description.setLineSpacing(2, 1.2f);
if(screenSize == Configuration.SCREENLAYOUT_SIZE_NORMAL)
holder.description.setVisibility(View.INVISIBLE);
else
holder.description.setText(itemList.get(MainActivity.KEY_description));
holder.description.setTypeface(MainActivity.tf2);
imageLoader.DisplayImage(itemList.get(MainActivity.KEY_image),
holder.thumb_image);
}
return convertView;
}
static class ViewHolder {
public TextView journal, title, description,smallDescription1, DateTime, statId;
ImageView thumb_image;
}
}
Is there any possible way I can reduce or remove this lagging?
If you are using xml then set the width and height of the ListView to match_parent.
I see that you use Threads to load images from url. I Recommend you to use Android Universal Image Loader Library. It is pretty flexible and less laggy. You can find the whole information Here
Also, it looks like you are creating objects in the getView method. This means that when each view is drawn the OS must go through all the work creating those objects for you. If are working with a large list (or small amt of ram) you will force the garbage collector to run, causing stuttering of your app. Try to move all the object creation to the constructor method so it will only run once. Then just reassign them as needed in your get view method.
This should provide a noticable speed improvement.
Good luck ;)
I got json data with image url. Now I want to get images from that image url from json data. So, how can i get image from that image url? Any suggestion for this?
I get following image url from json.
[{....,"image":"xyz.net/abc/img/p/9/0/9/7/9097-tonytheme_product.jpg",....},{....}]
I want to display above image url on dynamically created imageview.
I have following method for dynamially created imageview.
public void addImagesToView() {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Log.i("ArrayList In Image","" +arraylist);
for (int i = 0; i < arraylist.size(); i++) {
imageButton = new ImageView(this);
LinearLayout.LayoutParams params = new LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
// for setting image margin and spacing(left,top,right,bottom)
params.setMargins(60, 20, 5, 5);
imageButton.setLayoutParams(params);
horizontalOuterLayouthome.addView(imageButton);
// horizontalOuterLayouthome.setVisibility(View.INVISIBLE);
}
}
Try this..
for (int i = 0; i < arraylist.size(); i++) {
imageButton = new ImageView(this);
Bitmap bitmap = loadImage(arraylist.get(i));
imageButton.setImageBitmap(bitmap);
LinearLayout.LayoutParams params = new LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
// for setting image margin and spacing(left,top,right,bottom)
params.setMargins(60, 20, 5, 5);
imageButton.setLayoutParams(params);
horizontalOuterLayouthome.addView(imageButton);
// horizontalOuterLayouthome.setVisibility(View.INVISIBLE);
}
loadImage function
protected Bitmap loadImage(String utl2) {
// TODO Auto-generated method stub
Log.v("utl2--", utl2);
URL imageURL = null;
Bitmap bitmap = null;
try {
imageURL = new URL(utl2);
}
catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection connection = (HttpURLConnection) imageURL
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream inputStream = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
Try this code :
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.inSampleSize = 1;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
} catch (IOException e) {
Log.e(TAG, "Could not load Bitmap from: " + url);
} finally {
closeStream(in);
closeStream(out);
}
return bitmap;
}
Use below code of image loader class with image adapter.....
public class ImageLoader {
int image_size = 60;
MemoryCache memoryCache = new MemoryCache();
FileCache fileCache;
private Map<ImageView, String> imageViews = Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
ExecutorService executorService;
Context m_c;
boolean mFlag = false;
public ImageLoader(Context context) {
fileCache = new FileCache(context);
executorService = Executors.newFixedThreadPool(2);
m_c=context;
}
// final int stub_id = R.drawable.animation_loding;
public void DisplayImage(String url,Bitmap my_bmp, ImageView imageView, int ad_b_width, boolean b)
{
this.mFlag = b;
this.image_size = ad_b_width;
if(url!=null)
{
imageViews.put(imageView, url);
Bitmap bitmap = memoryCache.get(url);
if (bitmap != null)
{
if(mFlag)
{
int h = bitmap.getHeight();
int w = bitmap.getWidth();
int w1 = image_size;
if(w > w1)
{
int h1 = (h*w1)/w;
System.out.println("photos size :: "+w1 +" and "+h1);
//Bitmap bit = drawShadow(bitmap, 0, 1, 1, 1);
imageView.setLayoutParams(new LinearLayout.LayoutParams(w1, h1));
imageView.setScaleType(ScaleType.FIT_XY);
imageView.setImageBitmap(bitmap);
}
else
{
imageView.setLayoutParams(new LinearLayout.LayoutParams(w, h));
imageView.setScaleType(ScaleType.FIT_XY);
imageView.setImageBitmap(bitmap);
}
}
else
{
imageView.setScaleType(ScaleType.FIT_XY);
imageView.setImageBitmap(bitmap);
}
}
else
{
queuePhoto(url, imageView);
imageView.setScaleType(ScaleType.CENTER);
imageView.setBackgroundResource(R.drawable.region_images);
}
}
else if (my_bmp != null)
{
Bitmap bitmap=my_bmp;
if (bitmap != null)
{
if(mFlag)
{
int h = bitmap.getHeight();
int w = bitmap.getWidth();
int w1 = image_size;
if(w > w1)
{
int h1 = (h*w1)/w;
System.out.println("photos size :: "+w1 +" and "+h1);
//Bitmap bit = drawShadow(bitmap, 0, 1, 1, 1);
imageView.setLayoutParams(new LinearLayout.LayoutParams(w1, h1));
imageView.setScaleType(ScaleType.FIT_XY);
imageView.setImageBitmap(bitmap);
}
else
{
imageView.setLayoutParams(new LinearLayout.LayoutParams(w, h));
imageView.setScaleType(ScaleType.FIT_XY);
imageView.setImageBitmap(bitmap);
}
}
else
{
imageView.setScaleType(ScaleType.FIT_XY);
imageView.setImageBitmap(bitmap);
}
}
}
}
private void queuePhoto(String url, ImageView imageView)
{
PhotoToLoad p = new PhotoToLoad(url, imageView);
executorService.submit(new PhotosLoader(p));
}
private Bitmap getBitmap(String url) {
File f = fileCache.getFile(url);
// from SD cache
Bitmap b = decodeFile(f);
if (b != null)
return b;
// from web
try
{
Bitmap bitmap = null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) imageUrl
.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is = conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
bitmap = decodeFile(f);
return bitmap;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
// decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f)
{
try
{
// decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
//o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = this.image_size;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
Log.i("images scal ", String.valueOf(scale));
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
}
return null;
}
// Task for the queue
private class PhotoToLoad {
public String url;
public ImageView imageView;
public PhotoToLoad(String u, ImageView i) {
url = u;
imageView = i;
}
}
class PhotosLoader implements Runnable
{
PhotoToLoad photoToLoad;
PhotosLoader(PhotoToLoad photoToLoad)
{
this.photoToLoad = photoToLoad;
}
#Override
public void run() {
if (imageViewReused(photoToLoad))
return;
Bitmap bmp = getBitmap(photoToLoad.url);
memoryCache.put(photoToLoad.url, bmp);
if (imageViewReused(photoToLoad))
return;
BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad);
Activity a = (Activity) photoToLoad.imageView.getContext();
a.runOnUiThread(bd);
}
}
boolean imageViewReused(PhotoToLoad photoToLoad) {
String tag = imageViews.get(photoToLoad.imageView);
if (tag == null || !tag.equals(photoToLoad.url))
return true;
return false;
}
// Used to display bitmap in the UI thread
class BitmapDisplayer implements Runnable {
Bitmap bitmap;
PhotoToLoad photoToLoad;
public BitmapDisplayer(Bitmap b, PhotoToLoad p) {
bitmap = b;
photoToLoad = p;
}
public void run()
{
if (imageViewReused(photoToLoad))
return;
if (bitmap != null)
{
if(mFlag)
{
int h = bitmap.getHeight();
int w = bitmap.getWidth();
int w1 = image_size;
if(w > w1)
{
int h1 = (h*w1)/w;
System.out.println("photos size :: "+w1 +" and "+h1);
photoToLoad.imageView.setLayoutParams(new LinearLayout.LayoutParams(w1, h1));
photoToLoad.imageView.setScaleType(ScaleType.FIT_XY);
photoToLoad.imageView.setImageBitmap(bitmap);
}
else
{
photoToLoad.imageView.setLayoutParams(new LinearLayout.LayoutParams(w, h));
photoToLoad.imageView.setScaleType(ScaleType.FIT_XY);
photoToLoad.imageView.setImageBitmap(bitmap);
}
bitmap = null;
}
else
{
photoToLoad.imageView.setScaleType(ScaleType.FIT_XY);
photoToLoad.imageView.setImageBitmap(bitmap);
bitmap = null;
}
}
else
{
}
}
}
public void clearCache() {
memoryCache.clear();
fileCache.clear();
}
use Universal Image Loader it is good diaplay and cache the images https://github.com/nostra13/Android-Universal-Image-Loader
File cacheDir = StorageUtils.getOwnCacheDirectory(a, "your folder");
// Get singletone instance of ImageLoader
imageLoader = ImageLoader.getInstance();
// Create configuration for ImageLoader (all options are optional)
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(a)
// You can pass your own memory cache implementation
.discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation
.discCacheFileNameGenerator(new HashCodeFileNameGenerator())
.enableLogging()
.build();
// Initialize ImageLoader with created configuration. Do it once.
imageLoader.init(config);
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.stub_id)//display stub image
.cacheInMemory()
.cacheOnDisc()
.displayer(new RoundedBitmapDisplayer(20))
.build();
ImageView image=(ImageView)vi.findViewById(R.id.imageview);
imageLoader.displayImage(imageurl, image,options);
As Rich said picasso is a good option if you are receiving the url from JSon, it loads the image in a background async task very fast and easy to implement, for example:
String imageUrl = "xyz.net/abc/img/p/9/0/9/7/9097-tonytheme_product.jpg"; // this is an example, you can get this url from json
Picasso.with(this.getActivity()).load(imageUrl).into(imageView);
And that's it.
I've been working with custom listView containing 1 imageView and 3 textViews.The image is loaded from url,due to which slows down my scrolling.Any help is appreciated..
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) LayoutInflater
.from(context);
convertView = inflater.inflate(R.layout.customvenue, parent, false);
}
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream((InputStream) new URL(
VenueArray.get(position).get("VenueImage"))
.getContent());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
iconImage.setImageBitmap(bitmap);
return convertView;
}
Here VenueArray is hash map array that contains all data
add this class, it will help you out, works like a charm for me
public class ImageLoader {
MemoryCache memoryCache = new MemoryCache();
FileCache fileCache;
private Map<ImageView, String> imageViews = Collections
.synchronizedMap(new WeakHashMap<ImageView, String>());
private ProgressBar indicator;
public ImageLoader(Context context) {
photoLoaderThread.setPriority(Thread.NORM_PRIORITY - 1);
fileCache = new FileCache(context);
}
public void DisplayImage(String url, Context activity, ImageView imageView,
ProgressBar pbar) {
imageViews.put(imageView, url);
Bitmap bitmap = memoryCache.get(url);
if (bitmap != null) {
this.indicator = pbar;
indicator.setVisibility(View.INVISIBLE);
// imageView.setImageBitmap(getRoundedCornerBitmap(bitmap,13));
imageView.setImageBitmap(bitmap);
} else {
this.indicator = pbar;
indicator.setVisibility(View.INVISIBLE);
queuePhoto(url, activity, imageView);
imageView.setImageBitmap(null);
}
}
private void queuePhoto(String url, Context activity, ImageView imageView) {
// This ImageView may be used for other images before. So there may be
// some old tasks in the queue. We need to discard them.
photosQueue.Clean(imageView);
PhotoToLoad p = new PhotoToLoad(url, imageView);
synchronized (photosQueue.photosToLoad) {
photosQueue.photosToLoad.push(p);
photosQueue.photosToLoad.notifyAll();
}
// start thread if it's not started yet
if (photoLoaderThread.getState() == Thread.State.NEW)
photoLoaderThread.start();
}
private Bitmap getBitmap(String url) {
File f = fileCache.getFile(url);
// from SD cache
Bitmap b = decodeFile(f);
if (b != null)
return b;
// from web
try {
Bitmap bitmap = null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) imageUrl
.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
InputStream is = conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
bitmap = decodeFile(f);
return bitmap;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
// decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f) {
try {
// decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = 70;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
}
return null;
}
// Task for the queue
private class PhotoToLoad {
public String url;
public ImageView imageView;
public PhotoToLoad(String u, ImageView i) {
url = u;
imageView = i;
}
}
PhotosQueue photosQueue = new PhotosQueue();
public void stopThread() {
photoLoaderThread.interrupt();
}
// stores list of photos to download
class PhotosQueue {
private Stack<PhotoToLoad> photosToLoad = new Stack<PhotoToLoad>();
// removes all instances of this ImageView
public void Clean(ImageView image) {
for (int j = 0; j < photosToLoad.size();) {
if (photosToLoad.get(j).imageView == image)
photosToLoad.remove(j);
else
++j;
}
}
}
class PhotosLoader extends Thread {
public void run() {
try {
while (true) {
// thread waits until there are any images to load in the
// queue
if (photosQueue.photosToLoad.size() == 0)
synchronized (photosQueue.photosToLoad) {
photosQueue.photosToLoad.wait();
}
if (photosQueue.photosToLoad.size() != 0) {
PhotoToLoad photoToLoad;
synchronized (photosQueue.photosToLoad) {
photoToLoad = photosQueue.photosToLoad.pop();
}
Bitmap bmp = getBitmap(photoToLoad.url);
memoryCache.put(photoToLoad.url, bmp);
String tag = imageViews.get(photoToLoad.imageView);
if (tag != null && tag.equals(photoToLoad.url)) {
//System.out.println("indicator ::" + indicator);
BitmapDisplayer bd = new BitmapDisplayer(bmp,
photoToLoad.imageView, indicator);
Activity a = (Activity) photoToLoad.imageView
.getContext();
a.runOnUiThread(bd);
}
}
if (Thread.interrupted())
break;
}
} catch (InterruptedException e) {
// allow thread to exit
}
}
}
PhotosLoader photoLoaderThread = new PhotosLoader();
// Used to display bitmap in the UI thread
class BitmapDisplayer implements Runnable {
Bitmap bitmap;
ImageView imageView;
// private ProgressBar indicator1;
public BitmapDisplayer(Bitmap b, ImageView i, ProgressBar indicator) {
this.bitmap = b;
this.imageView = i;
// this.indicator1 = indicator;
}
public void run() {
if (bitmap != null) {
imageView.setImageBitmap(bitmap);
}
}
public void clearCache() {
memoryCache.clear();
fileCache.clear();
}
}
Use Universal Image Downloader to display the images.
It works fine for me hope will work for u...!