Images are reloading when scrolling listView in Android - android

I have a listView where I put a Title and a description with a picture and the pictures reloads when I scroll up, how could I resolve this problem? I tried some solutions but they failed...
Here is my code:
GETVIEW
public class ViewHolder extends ArrayAdapter<ListViewModel> {
public ViewHolder(Context context, List<ListViewModel> model) {
super(context, 0, model);
}
#Override
public View getView(int position, View cView, ViewGroup parent) {
if(cView == null){
cView = LayoutInflater.from(getContext()).inflate(R.layout.view_list_holder,parent, false);
}
ListViewHolder viewHold = (ListViewHolder) cView.getTag();
if(viewHold == null){
viewHold = new ListViewHolder();
viewHold.title = (TextView) cView.findViewById(R.id.title);
viewHold.content = (TextView) cView.findViewById(R.id.content);
viewHold.picture = (ImageView) cView.findViewById(R.id.picture);
cView.setTag(viewHold);
}
ListViewModel model = getItem(position);
viewHold.title.setText(model.getTitle());
viewHold.content.setText(model.getContent());
if (viewHold.picture != null)
new DownloadImageTask(viewHold.picture).execute(model.getPicture());
return cView;
}
private class ListViewHolder{
public TextView title;
public TextView content;
public ImageView picture;
}
}
ImageDownloader
public 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 myImage = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
myImage = BitmapFactory.decodeStream(in);
} catch (Exception e) {
e.printStackTrace();
}
return myImage;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}

Try using Volley instead of AsyncTask:
http://developer.android.com/training/volley/request.html
It has built in support for image caching as well as cancelling requests if a particular set of images are no longer needed if the user has scrolled away.

Could you not just save the images in a bitmap array in the activity? Then just display those. Therefore they never reload, they are just in activity to be used in the view?

Related

Android application doesn't run properly from the first try

Okay, so far my MainActivity for this simple app looks like this:
public class MainActivity extends AppCompatActivity {
ListView moviesListView;
ArrayList <Movie> moviesList;
MoviesAdapter moviesAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
moviesListView = (ListView) findViewById(R.id.moviesListView);
moviesList = new ArrayList<Movie>();
runProgram();
moviesAdapter = new MoviesAdapter(getApplicationContext(), R.layout.movies_layout, moviesList);
moviesListView.setAdapter(moviesAdapter);
}
private void runProgram() {
String url = "https://api.themoviedb.org/3/" +
"discover/movie?primary_release_date.gte=2016-02-01&primary_release_date.lte=2016-02-18" +
"&api_key=...............................";
JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
getMovies(response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
});
// Access the RequestQueue through your singleton class.
MySingleton.getInstance(this).addToRequestQueue(jsObjRequest);
}
private void getMovies(JSONObject response) {
JSONArray jsonArray = null;
String imageBaseURL = "http://image.tmdb.org/t/p/w500";
try {
jsonArray = response.getJSONArray("results");
for (int i = 0; i < 5; i++){
Movie movie = new Movie(jsonArray.getJSONObject(i).getString("title"),
jsonArray.getJSONObject(i).getString("overview"),
imageBaseURL + jsonArray.getJSONObject(i).getString("poster_path")
+ "?&api_key=6b54bd769063ac68b99fac57aa334eae");
moviesList.add(movie);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
What I am trying to do here is request movies from TMDB, and display them in a ListView. But my problem is that the first time you run the app, it doesn't display anything, you would only see white screen.
Now, if you try running it again, it would display the movies in the ListView. But if you clear the cache of the application and try running it one more time, you would see the white screen again... . I am really confused of what is going on here.
I've tried debugging it, and it seems like it is getting the data really fast, so whatever is happening is happening after that.
Also, here is the code of my Adapter in case:
public class MoviesAdapter extends ArrayAdapter<Movie> {
ArrayList<Movie> moviesList;
int adapterResource;
Context adapterContext;
LayoutInflater layoutInflator;
public MoviesAdapter(Context context, int resource, ArrayList<Movie> objects) {
super(context, resource, objects);
moviesList = objects;
adapterResource = resource;
adapterContext = context;
layoutInflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null){
convertView = layoutInflator.inflate(adapterResource, null);
viewHolder = new ViewHolder();
viewHolder.movieImage = (ImageView) convertView.findViewById(R.id.movieImage);
viewHolder.movieName = (TextView) convertView.findViewById(R.id.movieName);
viewHolder.movieOverview = (TextView) convertView.findViewById(R.id.movieOverview);
convertView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder) convertView.getTag();
}
new DownloadImageTask(viewHolder.movieImage).execute(moviesList.get(position).getImageURL());
viewHolder.movieName.setText(moviesList.get(position).getName());
viewHolder.movieOverview.setText(moviesList.get(position).getOverview());
return convertView;
}
public static class ViewHolder{
public ImageView movieImage;
public TextView movieName;
public TextView movieOverview;
}
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 mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception ex) {
Log.e("Error", ex.getMessage());
ex.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
}
Any help would be much appreciated.
Each time your getView() is called, you would start a new DownloadTask. That is simply wrong because the same image may already be downloaded/in the process of being downloaded
Use Universal Image loader or Picasso :
Instead of this :
new DownloadImageTask(viewHolder.movieImage).execute(moviesList.get(position).getImageURL());
Use :
Picasso.with(context)
.load(moviesList.get(position).getImageURL())
.into(viewHolder.movieImage);
Also, it seems that your getMovies() function is executed on main thread. Since you are parsing a JSON here, move it to background thread by using an AsyncTask
Thirdly, after you do getMovies(response);, you need to call notifyDataSetChange() on your moviesAdapter

Android load images into viewpager from web

I am loading images from drawable but it is not effective now i want to load images from web but i am stucked. I realy search for solutions but i cant success.
There is my Custom Adapter class:
int[] imageId = {R.drawable.first, R.drawable.second, R.drawable.third, R.drawable.fourth, R.drawable.fifth};
public Object instantiateItem(ViewGroup container, int position) {
global_position=position;
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
View viewItem = inflater.inflate(R.layout.image_item, container, false);
imageView = (ImageView) viewItem.findViewById(R.id.imageView);
imageView.setImageResource(imageId[position]);
((ViewPager)container).addView(viewItem);
return viewItem;
}
How can a turn this it can download images from web and load view pager.
You can use an AsyncTask to do that job without crashing your app.
class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView myImage //your image url;
public DownloadImageTask(ImageView myImage) {
this.myImage = myImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon;
}
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
}
}
To use this class just instanciate it like the others.
new DownloadImageTask((ImageView) findViewById(R.id.your_image_viewer))
.execute(imageURL);

Loading an image url into an item ListView using paging, Android

Im trying to make a lisView with two textView and an imageView (that come as a url Sting) on each item at the list but, the list is not scrolling as good as I want, because its taking too long to load the image url.
Im using an AsyncTask class for loading the the image but still it dosent look so good.
here is my code at int the ArrayAdapter class:
public class MySimpleArrayAdapter extends ArrayAdapter<Movie> {
final private Context context;
final private Movie[] movies;
ImageView movieIcon;
TextView name, description;
Bitmap bitmap;
public MySimpleArrayAdapter(Context context, Movie[] movies) {
super(context,R.layout.item_in_movielist, movies);
this.context = context;
this.movies = movies;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.item_in_movielist, parent, false);
name = (TextView) rowView.findViewById(R.id.tvMovieName);
description = (TextView) rowView.findViewById(R.id.tvMovieDescription);
movieIcon = (ImageView) rowView.findViewById(R.id.ivMovieIcon);
GetImageAsync getImageAsync = new GetImageAsync();
getImageAsync.imageView = movieIcon;
name.setText(movies[position].getMovieName());
description.setText(movies[position].getMovieDescription());
getImageAsync.execute(position);
return rowView;
}
public class GetImageAsync extends AsyncTask<Integer, Void, Bitmap> {
public ImageView imageView;
#Override
protected void onPostExecute(Bitmap bitmap1) {
imageView.setImageBitmap(bitmap1);
}
#Override
protected Bitmap doInBackground(Integer... params) {
URL url = null;
try {
url = new URL(movies[params[0]].getMovieImgURL());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
return BitmapFactory.decodeStream(input);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
}
I understood that this is not the way to do that, I`m looking for changing my code into "Paging" and I want to do it right.
any tips what can I do ?
P.S
If you can show me how to add Paging to this code it will be great.
Thanks!
Picasso.with(mContext)
.load(img.get(pos).replaceAll(" ", "%20"))
.placeholder(R.drawable.ic_launcher)
.error(R.drawable.ic_launcher)
.noFade().resize(70, 70)
.into(v.image);

Images are repeating in ListView

I have implemented android app which should download images from server and display them in ListView, but very interesting thing occures while images are downloading
As you can see in video pictures which haven't been downloaded yet are represented by those which have been already downloaded. How that can happen? I've thinking about it almost two days.
http://www.youtube.com/watch?v=lxY-HAuJO0o&feature=youtu.be
here is my code of ListView adapter.
public class MoviesAdapter extends ArrayAdapter<ParkCinema> {
private ArrayList<ParkCinema> movieDataItems;
private Activity context;
public MoviesAdapter(Activity context, int textViewResourceId, ArrayList<ParkCinema> movieDataItems) {
super(context, textViewResourceId, movieDataItems);
this.context = context;
this.movieDataItems = movieDataItems;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.movie_data_row, null);
}
ParkCinema movie = movieDataItems.get(position);
if (movie!=null){
ImageView imageView = (ImageView) convertView.findViewById(R.id.movie_thumb_icon);
String url = movie.poster();
if (url!=null) {
Bitmap bitmap = fetchBitmapFromCache(url);
if (bitmap==null) {
new BitmapDownloaderTask(imageView).execute(url);
}
else {
imageView.setImageBitmap(bitmap);
}
}
}
return convertView;
}
private LinkedHashMap<String, Bitmap> bitmapCache = new LinkedHashMap<String, Bitmap>();
private void addBitmapToCache(String url, Bitmap bitmap) {
if (bitmap != null) {
synchronized (bitmapCache) {
bitmapCache.put(url, bitmap);
}
}
}
private Bitmap fetchBitmapFromCache(String url) {
synchronized (bitmapCache) {
final Bitmap bitmap = bitmapCache.get(url);
if (bitmap != null) {
return bitmap;
}
}
return null;
}
private class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap> {
private String url;
private final WeakReference<ImageView> imageViewReference;
public BitmapDownloaderTask(ImageView imageView) {
imageViewReference = new WeakReference<ImageView>(imageView);
}
#Override
protected Bitmap doInBackground (String... source) {
url = source[0];
Bitmap image;
try{
image = BitmapFactory.decodeStream(new URL(url).openConnection().getInputStream());
return image;
}
catch(Exception e){Log.e("Error", e.getMessage()); e.printStackTrace();}
return null;
}
#Override
protected void onPostExecute(Bitmap bitmap) {
addBitmapToCache(url, bitmap);
imageViewReference.get().setImageBitmap(bitmap);
}
}
}
Edit 3:
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.movie_data_row, null);
}
ParkCinema movie = movieDataItems.get(position);
ImageView imageView = (ImageView) convertView.findViewById(R.id.movie_thumb_icon);
if (movie!=null){
String url = movie.poster();
if (url != null) {
Bitmap bitmap = fetchBitmapFromCache(url);
if (bitmap == null) {
imageView.setImageResource(R.drawable.no_image);
new BitmapDownloaderTask(imageView).execute(url);
}
else {
imageView.setImageBitmap(bitmap);
}
}
else {
imageView.setImageResource(R.drawable.no_image);
}
}
else {
imageView.setImageResource(R.drawable.no_image);
}
return convertView;
}
Aha! I think I may know the issue. Right now, your getView method sets your ImageView like this:
Gets movie object at position
Pulls out the movie's thumbnail url
Using that url, it tries to find the image in the cache
If it finds the image, it sets it
If it can't find the image, it starts an async network request to go get it, and sets it after it gets downloaded.
Your issus arises since ListView reuses its rows' Views. When the first View scrolls off the screen, rather than inflate a new one, ListView passes the now offscreen row's View in as convertView for you to reuse (this is for efficiency).
When your getView gets a convertView that is getting reused, its ImageView has already been set from the row that had it before, so you see the old image from the offscreen row's View. With your current getView process, you check for the new row's image, and it doesn't find it in the cache, it starts a request to download it. While it is downloading, you see the old image until you get the new image.
To fix this, you need to make sure you set every field in the row's View immediately, to make sure you don't have any Views showing stale data. I would suggest you set the ImageView to the default drawable resource (you have set in your R.layout.movie_data_row) while you wait for the network download to get the image.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.movie_data_row, null);
}
ParkCinema movie = movieDataItems.get(position);
ImageView imageView = (ImageView) convertView.findViewById(R.id.movie_thumb_icon);
if (movie != null) {
String url = movie.poster();
if (url != null) {
Bitmap bitmap = fetchBitmapFromCache(url);
if (bitmap == null) {
// Set the movie thumbnail to the default icon while we load
// the real image
imageView.setImageResource(R.drawable.movie_thumb_icon);
new BitmapDownloaderTask(imageView).execute(url);
}
else {
// Set the image to the bitmap we get from the cache
imageView.setImageBitmap(bitmap);
}
}
else {
// Set the movie thumbnail to the default icon, since it doesn't
// have a thumbnail URL
imageView.setImageResource(R.drawable.movie_thumb_icon);
}
}
else {
// Set the movie thumbnail to the default icon, since there's no
// movie data for this row
imageView.setImageResource(R.drawable.movie_thumb_icon);
}
-Edit-
Updated to be even more robust, using your drawable. You also have an issue with your BitmapDownloaderTask, it does not handle errors/null. Try adding this as well.
#Override
protected void onPostExecute(Bitmap bitmap) {
addBitmapToCache(url, bitmap);
if (bitmap == null) {
// Set the movie thumbnail to the default icon, since an error occurred while downloading
imageViewReference.get().setImageResource(R.drawable.movie_thumb_icon);
}
else {
imageViewReference.get().setImageBitmap(bitmap);
}
}
i had this issue and implemented lruCache ...i believe you need api 12 and above or use the compatiblity v4 library. lurCache is fast memory but it also has a budget, so if your worried about that you can use a diskcache ...its all described here http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
I'll now provide my implementation which is a singleton i call from anywhere like this:
//where first is a string and other is a imageview to load
DownloadImageTask.getInstance().loadBitmap(avatarURL, iv_avatar);
here's the ideal code to cache and then call the above in getView of an adapter when retrieving the web image:
public class DownloadImageTask {
private LruCache<String, Bitmap> mMemoryCache;
/* create a singleton class to call this from multiple classes */
private static DownloadImageTask instance = null;
public static DownloadImageTask getInstance() {
if (instance == null) {
instance = new DownloadImageTask();
}
return instance;
}
//lock the constructor from public instances
private DownloadImageTask() {
// Get max available VM memory, exceeding this amount will throw an
// OutOfMemory exception. Stored in kilobytes as LruCache takes an
// int in its constructor.
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
// Use 1/8th of the available memory for this memory cache.
final int cacheSize = maxMemory / 8;
mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
#Override
protected int sizeOf(String key, Bitmap bitmap) {
// The cache size will be measured in kilobytes rather than
// number of items.
return bitmap.getByteCount() / 1024;
}
};
}
public void loadBitmap(String avatarURL, ImageView imageView) {
final String imageKey = String.valueOf(avatarURL);
final Bitmap bitmap = getBitmapFromMemCache(imageKey);
if (bitmap != null) {
imageView.setImageBitmap(bitmap);
} else {
imageView.setImageResource(R.drawable.ic_launcher);
new DownloadImageTaskViaWeb(imageView).execute(avatarURL);
}
}
private void addBitmapToMemoryCache(String key, Bitmap bitmap) {
if (getBitmapFromMemCache(key) == null) {
mMemoryCache.put(key, bitmap);
}
}
private Bitmap getBitmapFromMemCache(String key) {
return mMemoryCache.get(key);
}
/* a background process that opens a http stream and decodes a web image. */
class DownloadImageTaskViaWeb extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTaskViaWeb(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
addBitmapToMemoryCache(String.valueOf(urldisplay), mIcon);
return mIcon;
}
/* after decoding we update the view on the mainUI */
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
}
Views are reused for performance with Adapters. You should use another approch .
You have to have a class holder which reuse your views. In your case you class should be something like this:
public class MoviesAdapter extends ArrayAdapter<ParkCinema> {
private ArrayList<ParkCinema> movieDataItems;
private Activity context;
public MoviesAdapter(Activity context, int textViewResourceId, ArrayList<ParkCinema> movieDataItems) {
super(context, textViewResourceId, movieDataItems);
this.context = context;
this.movieDataItems = movieDataItems;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.movie_data_row, null);
holder = new ViewHolder();
holder.imageView = (BarImageView) convertView.findViewById(R.id.movie_thumb_icon);
} else {
holder = (ViewHolder) convertView.getTag();
}
ParkCinema movie = movieDataItems.get(position);
if (movie!=null){
String url = movie.poster();
if (url!=null) {
Bitmap bitmap = fetchBitmapFromCache(url);
if (bitmap==null) {
new BitmapDownloaderTask(imageView).execute(url);
}
else {
imageView.setImageBitmap(bitmap);
}
}
}
return convertView;
}
private LinkedHashMap<String, Bitmap> bitmapCache = new LinkedHashMap<String, Bitmap>();
private void addBitmapToCache(String url, Bitmap bitmap) {
if (bitmap != null) {
synchronized (bitmapCache) {
bitmapCache.put(url, bitmap);
}
}
}
private Bitmap fetchBitmapFromCache(String url) {
synchronized (bitmapCache) {
final Bitmap bitmap = bitmapCache.get(url);
if (bitmap != null) {
return bitmap;
}
}
return null;
public static class ViewHolder {
ImageView imageView;
}
}
I have spent hours trying to figure this one out as well...Thanks to Steven Byle's solution...
Here is my solution to something similar when a user selects an item from a list:
adapter.setSelectedIndex(position);
then in the custom adapter:
public void setSelectedIndex(int ind)
{
selectedIndex = ind;
notifyDataSetChanged();
}
and then finally in the getView method of the adapter:
if(selectedIndex!= -1 && position == selectedIndex)
{
holder.tab.setBackgroundColor(Color.BLACK);
}
else{
holder.tab.setBackgroundColor(Color.DKGRAY);
}
So in conclusion make sure you assign default values
In my case i used Picasso library instead of AsyncTask for downloading image.
enter link description here
Also write if else condition, that is set null to image if url is not available
instead of using the convertview object create a new view each time.
View localView = ((LayoutInflater)parentscreen.getSystemService("layout_inflater")).inflate(R.layout.activity_list_row, null);
By inflating as above.

scrolling listfragment not fluent with photo

i have a listfragment whith users photo. When i scroll my list it is slow. Then i load my photo async:
public class UserArrayAdapter extends ArrayAdapter<Friend>{
.
.
.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_user, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
textView.setText(friends.get(position).getName());
String s = friends.get(position).getName();
String Contact_Id = friends.get(position).getID();
new BackgroundLoadPhoto(context, Contact_Id, rowView).execute();
return rowView;
}
class BackgroundLoadPhoto extends AsyncTask<Void, Void, Void> {
Context mContext;
String Contact_Id;
View rowView;
Bitmap photo;
ImageView profile;
public BackgroundLoadPhoto(Context mContext,String Contact_Id,View rowView){
this.mContext=mContext;
this.Contact_Id=Contact_Id;
this.rowView=rowView;
profile = (ImageView)rowView.findViewById(R.id.logo);
}
#Override
protected void onPostExecute(Void result) {
//set real user immage
if(photo!=null){
profile.setImageBitmap(photo);
}
}
#Override
protected void onPreExecute() {
//set default photo
profile.setImageResource(R.drawable.search_marker);
}
#Override
protected Void doInBackground(Void... params) {
//get user photo in background
photo=getPhoto(Contact_Id);
return null;
}
private Bitmap getPhoto(String Contact_Id){
Bitmap my_btmp = null;
//only if it is a real user..
if(Contact_Id!="-1"){
Uri my_contact_Uri = Uri.
withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(Contact_Id));
InputStream photo_stream = ContactsContract
.Contacts.openContactPhotoInputStream(mContext.getContentResolver(),my_contact_Uri);
BufferedInputStream buf =new BufferedInputStream(photo_stream);
my_btmp = BitmapFactory.decodeStream(buf);
}
return my_btmp;
}
}
now i can scrolling my list fluent..but i can see photo after a while..i don't like! how i can do? thanks!
you're loading the bitmaps asynchronously and when you scroll they're not ready yet so you see them once they're ready.
you can improve performance by using a memory cache for the bitmaps, such as http://developer.android.com/reference/android/util/LruCache.html
see this - https://developers.google.com/events/io/sessions/gooio2012/103/

Categories

Resources