Display Image in Listview with AsyncTask - java

I am trying to display images in a listview with AsyncTask but I am getting an error. Saying...
Attempt to invoke virtual method 'java.lang.String
java.lang.Object.toString()' on a null object reference
This is error is happening on
public LoadImage(ImageView imv) {
this.imv = imv;
this.path = imv.getTag().toString();
}
And
LoadImage loadImage = new LoadImage(imageView);
Bitmap bitmap = loadImage.doInBackground();
Here is LoadImage.java
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Environment;
import android.view.View;
import android.widget.ImageView;
import java.io.File;
/**
* Created by Yudii on 27/05/2016.
*/
class LoadImage extends AsyncTask<Object, Void, Bitmap>{
private ImageView imv;
private String path;
public LoadImage(ImageView imv) {
this.imv = imv;
this.path = imv.getTag().toString();
}
#Override
protected Bitmap doInBackground(Object... params) {
Bitmap bitmap = null;
File file = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + path);
if (file.exists()) {
bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
}
return bitmap;
}
#Override
protected void onPostExecute(Bitmap result) {
if (!imv.getTag().toString().equals(path)) {
/* The path is not same. This means that this
image view is handled by some other async task.
We don't do anything and return. */
// return;
}
if(result != null && imv != null){
imv.setVisibility(View.VISIBLE);
imv.setImageBitmap(result);
}else{
imv.setVisibility(View.GONE);
}
}
}
And here is CustomList.java
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class CustomList extends ArrayAdapter<String>{
private final Activity context;
private final Integer[] imageId;
public CustomList(Activity context,
String[] web, Integer[] imageId) {
super(context, R.layout.list_single, web);
this.context = context;
this.imageId = imageId;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, null, true);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
LoadImage loadImage = new LoadImage(imageView);
Bitmap bitmap = loadImage.doInBackground();
imageView.setImageResource(imageId[position]);
return rowView;
}
}
And here is MainActivity.java
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
ListView list;
String[] web = {
"Google Plus",
"Twitter",
"Tall",
};
Integer[] imageId = {
R.drawable.gggggggggg,
R.drawable.hhhhhhhhhh,
R.drawable.ttttttttttt
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomList adapter = new
CustomList(MainActivity.this, web, imageId);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
}
});
}
}

You need to change loadImage.execute(); instead of Bitmap bitmap = loadImage.doInBackground();

Why do you need AsyncTask? Try to use library Picasso and open images by one string:
File file = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + path);
Picasso.with(context).load(file).into(imageView);
Bitmap objects are very weight and you need to clean memory after each Bitmap image for prevent OutOfMemoryError. I recommend you to use libraries.

Add this in your dependencies :-
compile 'com.squareup.picasso:picasso:2.5.2'
and then load your image :-
Picasso.with(this)
.load(drawerImage) // ImagePath
.placeholder( R.mipmap.download)
.error( R.mipmap.add_photo)
.into(imageDrawerView); //ImageView

1,Reason:
As I see, in your getView function:
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
// You need to set tag to your image view
// Ex: imageView.setTag("your_image_file_path") below this line
your imageView has no tag so the below code will get NullPointerException.
public LoadImage(ImageView imv) {
this.imv = imv;
this.path = imv.getTag().toString(); // Crashed here.
}
Also, I see in your getView function has this code:
imageView.setImageResource(imageId[position]);
That means "your ImageView will set resource corresponding to defined imageId array". So you no need AsyncTask class, let remove it and change your getView like below:
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, parent, false);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
// Remove below code
// LoadImage loadImage = new LoadImage(imageView);
// Bitmap bitmap = loadImage.doInBackground();
imageView.setImageResource(imageId[position]);
return rowView;
}
Your Asynctask class is needed whenever you use path of image file, not drawable resources.
2, Improvement:
You can learn more about ViewHolder pattern when using ListView, RecyclerView to improve performance, from Google Search. Below is an example with your current code:
#Override
public View getView(int position, View view, ViewGroup parent) {
View holder = null;
if (view == null) {
holder = new ViewHolder();
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.list_single, parent, false);
holder.imageView = (ImageView) view.findViewById(R.id.img);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.imageView.setImageResource(imageId[position]);
return view;
}
static class ViewHolder { // should be static class
ImageView imageView;
}

Related

Loading Glide image

I am working on developing an app to play songs, I use Glide to get the image of the song and display it in the ListView,
the first problem
when I scrolling in my ListView the application becomes slow, and I have another problem which is when I scrolling Up the Glide load the images that have already been loaded Is there a way to fix these problems?
here is my code to get the image of songs
`
import android.content.Context;
import android.media.MediaMetadataRetriever;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
public class ListAdapter
extends BaseAdapter {
private Context context;
private static ArrayList<ListItemFromPhone> newList = new ArrayList<>();
ListAdapter(){}
ListAdapter(Context context, ArrayList<ListItemFromPhone> list) {
notifyDataSetChanged();
this.context = context;
newList = list;
}
#Override
public int getCount() {
return newList.size();
}
#Override
public Object getItem(int position) {
return newList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(R.layout.view,null);
TextView textView = view.findViewById(R.id.textView);
final ImageView imageView = view.findViewById(R.id.imageView);
byte [] imgArtist = getImg(newList.get(position).data);
if (imgArtist != null){
Glide.with(context).asBitmap()
.load(imgArtist)
.placeholder(R.drawable.music_img)
.into(imageView);
}
else imageView.setImageResource(R.drawable.music_img);
textView.setText(newList.get(position).name);
return view;
}
private byte[] getImg(String path){
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(path);
byte[] img = retriever.getEmbeddedPicture();
retriever.release();
return img;
}
}
To avoid this problem you must use RecyclerView. There are a lot of tutorials on youtube on how to implement it.

Attempt to get length of null array - retrieve bitmap image from sqlite DB

I am trying to retrieve an image from an SQLite database. The image is stored as a BLOB and trying to retrieve it using an array. I'm not sure why its doing this. The permissions are all set correctly.
package com.example.joao_.quizathonegroupteamproject.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.joao_.quizathonegroupteamproject.DatabaseClasses.User;
import com.example.joao_.quizathonegroupteamproject.R;
import java.util.ArrayList;
/**
* Created by Quoc Nguyen on 13-Dec-16.
*/
public class UserListAdapter extends BaseAdapter {
private Context context;
private int layout;
private ArrayList<User> foodsList;
public UserListAdapter(Context context, int layout, ArrayList<User> foodsList) {
this.context = context;
this.layout = layout;
this.foodsList = foodsList;
}
#Override
public int getCount() {
return foodsList.size();
}
#Override
public Object getItem(int position) {
return foodsList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
private class ViewHolder{
ImageView imageView;
}
#Override
public View getView(int position, View view, ViewGroup viewGroup) {
View row = view;
ViewHolder holder = new ViewHolder();
if(row == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(layout, null);
holder.imageView = (ImageView) row.findViewById(R.id.imgFood);
row.setTag(holder);
}
else {
holder = (ViewHolder) row.getTag();
}
User food = foodsList.get(position);
byte[] tblUsersImage = food.getImage();
Bitmap bitmap = BitmapFactory.decodeByteArray(tblUsersImage, 0, tblUsersImage.length);
holder.imageView.setImageBitmap(bitmap);
return row;
}
}
You can retrieve BLOB image and stored it in Byte array like this:
byte[] array = cursor.getBlob(columnIndex);
Bitmap bitmap = BitmapFactory.decodeByteArray(array, 0 ,array.length);
and then set it into image view
holder.imageView.setImageBitmap(bitmap);
you didn't provide your SQLite database class but i think , your food.getImage(); return null value .
check dataInsert method OR getData in your SQLite db !

Adding Drawables from assets into ListView

I have added many images in the assets folder and want to get those images from there and add it into a ListView.
When i am executing the app everything is perfect but the image container of the listview appears blank(transparent).
Output
The Java File:
package com.basil.victor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import java.io.IOException;
import java.io.InputStream;
public class Events extends Fragment {
private ListView listEvent;
String eventname[]={
"Name",
"of",
"the",
"events",
"are",
"present",
"here"
};
String eventlogoname[]={
"Logo",
"name",
"of",
"events",
"are",
"present",
"here"
};
Drawable[] arr=new Drawable[7];
String eventsubtitle []={
"Subtitles",
"of",
"the",
"events",
"are",
"present",
"here"
};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_events, null);
for(int i=0;i<7;i++) {
try {
InputStream stream = getContext().getAssets().open(eventlogoname[i] + ".jpg");
Drawable el = Drawable.createFromStream(stream, null);
arr[i] = el;
} catch (IOException e) {
e.printStackTrace();
}
}
EventList adapter = new
EventList(getActivity(), eventname, arr, eventsubtitle);
//ListView lv = (ListView)rootView.
listEvent=(ListView)view.findViewById(R.id.listEvent);
listEvent.setAdapter(adapter);
return view;
}
}
ListView adapter:
package com.basil.victor;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class EventList extends ArrayAdapter<String>{
private final Activity context;
private final String[] title;
private final Drawable[] banner;
private final String[] subtitle;
public EventList(Activity context,
String[] title, Drawable[] banner, String[] subtitle) {
super(context, R.layout.list_single, title);
this.context = context;
this.title = title;
this.banner = banner;
this.subtitle = subtitle;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.event_row, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.event_title);
ImageView imageView = (ImageView) rowView.findViewById(R.id.event_banner);
TextView subTitle = (TextView) rowView.findViewById(R.id.event_subtitle);
txtTitle.setText(title[position]);
imageView.setImageDrawable(banner[position]);
subTitle.setText(subtitle[position]);
return rowView;
}
}
If you're getting a FileNotFound Exception,
Check if the image is inside a subfolder in assets and add the appropriate path
Check the case of the extension (I faced this problem for a long time before I realized my image had a .JPG (Uppercase) extension)
The final string that's being searched, check for random whitespaces
Why are using asset folder for images? if it is not necessary then you can use drawable for same put all images in drawable folder and
package com.basil.victor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import java.io.IOException;
import java.io.InputStream;
public class Events extends Fragment {
private ListView listEvent;
String eventname[]={
"Name",
"of",
"the",
"events",
"are",
"present",
"here"
};
int[] arr=new int[7];
String eventsubtitle []={
"Subtitles",
"of",
"the",
"events",
"are",
"present",
"here"
};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_events, null);
arr[]={
R.drawable.Logo,
R.drawable.name,
R.drawable.of,
R.drawable.events,
R.drawable.are,
R.drawable.present,
R.drawable.here
};
EventList adapter = new
EventList(getActivity(), eventname, arr, eventsubtitle);
//ListView lv = (ListView)rootView.
listEvent=(ListView)view.findViewById(R.id.listEvent);
listEvent.setAdapter(adapter);
return view;
}
}
adapter:
package com.basil.victor;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class EventList extends ArrayAdapter<String>{
private final Activity context;
private final String[] title;
private final int[] banner;
private final String[] subtitle;
public EventList(Activity context,
String[] title, int[] banner, String[] subtitle) {
super(context, R.layout.list_single, title);
this.context = context;
this.title = title;
this.banner = banner;
this.subtitle = subtitle;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.event_row, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.event_title);
ImageView imageView = (ImageView) rowView.findViewById(R.id.event_banner);
TextView subTitle = (TextView) rowView.findViewById(R.id.event_subtitle);
txtTitle.setText(title[position]);
imageView.setImageResource((banner[position]);
subTitle.setText(subtitle[position]);
return rowView;
}
}
Do it like this
class MainActivity extends AppCompatActivity {
private ListView listEvent;
String arras[]={
"Logo",
"name",
"of",
"events",
"are",
"present",
"here"
};
Drawable[] arr=new Drawable[5];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EventList adapter = new EventList(MainActivity.this, arras);
//ListView lv = (ListView)rootView.
listEvent=(ListView)findViewById(R.id.listEvent);
listEvent.setAdapter(adapter);
}
}
And Adapter class is like this
public class EventList extends BaseAdapter {
LayoutInflater inflater;
private final Activity context;
String arras[];
public EventList(Activity context,String[] arras) {
super();
this.context = context;
this.arras = arras;
inflater = (LayoutInflater) this.context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return arras.length;
}
#Override
public Object getItem(int position) {
return arras.length;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View myView, ViewGroup parent) {
if (myView == null) {
myView = inflater.inflate(R.layout.event_row, null, true);
}
ImageView imageView = (ImageView) myView.findViewById(R.id.event_banner);
try {
InputStream stream = context.getAssets().open(arras[position] + ".jpg");
Drawable el = Drawable.createFromStream(stream, null);
imageView.setImageDrawable(el);
} catch (IOException e) {
e.printStackTrace();
}
return myView;
}
}
Use a ViewHolder
ViewHolder makes the list view to load faster by caching the views. If it look up the view every time by calling the findViewById(), it will be very slow.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder; // to reference the child views for later actions
if (v == null) {
LayoutInflater vi =
(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.mainrow, null);
// cache view fields into the holder
holder = new ViewHolder();
holder.ImageView = (ImageView) v.findViewById(R.id.event_banner);
// associate the holder with the view for later lookup
v.setTag(holder);
} else {
// view already exists, get the holder instance from the view
holder = (ViewHolder) v.getTag();
}
// no local variables with findViewById here
// use holder.ImageView where you were
// using the local variable nameText before
try {
InputStream stream = context.getAssets().open(arras[position] + ".jpg");
Drawable el = Drawable.createFromStream(stream, null);
holder.ImageView.setImageDrawable(el);
} catch (IOException e) {
e.printStackTrace();
}
return v;
}
// somewhere else in your class definition
static class ViewHolder {
ImageView imageView;
}

How to show all images in this gallery app?

I'm making a gallery application.
I have the code for that task, but I'd like to show all SD internal storage images. Only I can see a specific folder in my application, I understand why. This is a method that I found on the internet.
MainActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import com.instinctcoder.gridview.R;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private GridView gridView;
private GridViewAdapter gridAdapter;
private List<String> imageItems;
private String imagePath="";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//location where photo saved
imagePath = Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera";//I'd like to add more Folders, but it doesn't work
imageItems = new ArrayList<>();
getImages();
gridView = (GridView) findViewById(R.id.gridView);
gridAdapter = new GridViewAdapter(this, R.layout.grid_item, imageItems);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
String imagePath = parent.getAdapter().getItem(position).toString();
Intent intent = new Intent(MainActivity.this, DetailActivity.class);
intent.putExtra("imagePath", imagePath);
startActivity(intent);
}
});
gridView.setAdapter(gridAdapter);
}
private List<String> getImages(){
new File(imagePath ).mkdirs();
File fileTarget = new File(imagePath);
File[] files = fileTarget.listFiles();
imageItems.clear();
if (files!=null){
for (File file: files){
imageItems.add(file.getAbsolutePath());
}
}
return imageItems;
}
}
GridViewAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.instinctcoder.gridview.R;
import java.util.ArrayList;
import java.util.List;
public class GridViewAdapter extends ArrayAdapter {
private Context context;
private List<String> data = new ArrayList<String>();
private int resourceId;
private LayoutInflater inflater ;
public GridViewAdapter(Context context, int resourceId, List<String> data) {
super(context, resourceId, data);
this.resourceId = resourceId;
this.context = context;
this.data = data;
inflater = LayoutInflater.from(context);
}
static class ViewHolder {
ImageView image;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null) {
row = inflater.inflate(resourceId, parent, false);
holder = new ViewHolder();
holder.image = (ImageView) row.findViewById(R.id.image);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
Glide.with(context)
.load("file://" + data.get(position))
.fitCenter()
.centerCrop()
.into(holder.image);
return row;
}
Detailactivity.java
package com.instinctcoder.gridview.Galeria;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.instinctcoder.gridview.R;
public class DetailActivity extends AppCompatActivity {
Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
String imagePath = getIntent().getStringExtra("imagePath");
ImageView picturesView = (ImageView) findViewById(R.id.image);
Glide.with(DetailActivity.this)
.load("file://" + imagePath)
.fitCenter()
.centerCrop()
.into(picturesView);
}
}
In manifest I have declared permissions storage, parent activity.
Also, I'm add glide library on gradle.

caching images from json in listview?

I followed this lesson here which talk about json parsing and image loading in list view the example works good but it has a problem in imageview when scrolling up & down all images reloading so can anyone solve this problem?
this the adapter of listview
package com.wingnity.jsonparsingtutorial;
import java.io.InputStream;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ActorAdapter extends ArrayAdapter<Actors> {
ArrayList<Actors> actorList;
LayoutInflater vi;
int Resource;
ViewHolder holder;
public ActorAdapter(Context context, int resource, ArrayList<Actors> objects) {
super(context, resource, objects);
vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Resource = resource;
actorList = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// convert view = design
View v = convertView;
if (v == null) {
holder = new ViewHolder();
v = vi.inflate(Resource, null);
holder.imageview = (ImageView) v.findViewById(R.id.ivImage);
holder.tvName = (TextView) v.findViewById(R.id.tvName);
holder.tvDescription = (TextView) v.findViewById(R.id.tvDescriptionn);
holder.tvDOB = (TextView) v.findViewById(R.id.tvDateOfBirth);
holder.tvCountry = (TextView) v.findViewById(R.id.tvCountry);
holder.tvHeight = (TextView) v.findViewById(R.id.tvHeight);
holder.tvSpouse = (TextView) v.findViewById(R.id.tvSpouse);
holder.tvChildren = (TextView) v.findViewById(R.id.tvChildren);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.imageview.setImageResource(R.drawable.ic_launcher);
new DownloadImageTask(holder.imageview).execute(actorList.get(position).getImage());
holder.tvName.setText(actorList.get(position).getName());
holder.tvDescription.setText(actorList.get(position).getDescription());
holder.tvDOB.setText("B'day: " + actorList.get(position).getDob());
holder.tvCountry.setText(actorList.get(position).getCountry());
holder.tvHeight.setText("Height: " + actorList.get(position).getHeight());
holder.tvSpouse.setText("Spouse: " + actorList.get(position).getSpouse());
holder.tvChildren.setText("Children: " + actorList.get(position).getChildren());
return v;
}
static class ViewHolder {
public ImageView imageview;
public TextView tvName;
public TextView tvDescription;
public TextView tvDOB;
public TextView tvCountry;
public TextView tvHeight;
public TextView tvSpouse;
public TextView tvChildren;
}
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 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) {
bmImage.setImageBitmap(result);
}
}
}
Used the Android Universal Image-Loader This is best
Declare
private ImageLoader imageLoader1;
On Create
imageLoader1 = ImageLoader.getInstance();
imageLoader1.init(ImageLoaderConfiguration.createDefault(getActivity()));
no_image here a drawable image without any image load in Cache
DisplayImageOptions
options = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.showImageOnLoading(R.drawable.no_image) // resource or drawable
.showImageForEmptyUri(R.drawable.no_image) // resource or drawable
.showImageOnFail(R.drawable.no_image)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
imageLoader1.displayImage(yourpath.replace(" ", "%20"), ivprofile, options);
You Can Use Piccaso Also and Glide also
here is the code after editing it works like a charm
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
public class ActorAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Actors> movieItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public ActorAdapter(Activity activity, List<Actors> movieItems) {
this.activity = activity;
this.movieItems = movieItems;
}
#Override
public int getCount() {
return movieItems.size();
}
#Override
public Object getItem(int location) {
return movieItems.get(location);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_item, null);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
NetworkImageView thumbNail = (NetworkImageView) convertView
.findViewById(R.id.flag);
TextView title = (TextView) convertView.findViewById(R.id.rank);
// getting movie data for the row
Actors m = movieItems.get(position);
// thumbnail image
thumbNail.setImageUrl(m.getImage(), imageLoader);
// title
title.setText(m.getName());
return convertView;
}
}

Categories