Reputation: 3825
I am trying to load the thumbnails of images and videos in the List view. While I am making their thumbnails, I provide a default thumbnail so that the task is easily done and therefore the thumbnail make and and load is put in a different thread. But the problem is the application crashes/exits.
public View getView(final int position, View convertView, ViewGroup parent)
{
File file=new File(String.valueOf(filenames[position]));
if(file.isDirectory()){
img=R.drawable.folder;
}
else{
img=getImage(filenames[position]);
}
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflator = LayoutInflater.from(getContext());
convertView = inflator.inflate(R.layout.list_item, null);
viewHolder = new ViewHolder();
viewHolder.tv = (TextView) convertView.findViewById(R.id.textView1);
viewHolder.cb = (CheckBox) convertView.findViewById(R.id.checkBox1);
viewHolder.iv= (ImageView)convertView.findViewById(R.id.imageView);
viewHolder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag.
myList.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
}
});
convertView.setTag(viewHolder);
convertView.setTag(R.id.textView1, viewHolder.tv);
convertView.setTag(R.id.checkBox1, viewHolder.cb);
convertView.setTag(R.id.checkBox1, viewHolder.iv);
}
else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.iv.getLayoutParams().height = 80;
viewHolder.iv.getLayoutParams().width = 80;
viewHolder.cb.setTag(position); // This line is important.
final ViewHolder finalViewHolder = viewHolder;
Thread t2 = new Thread(){
public void run(){
if((String.valueOf(filenames[position])).contains(".jpeg")||String.valueOf(filenames[position]).contains(".jpg")||String.valueOf(filenames[position]).contains(".png")||String.valueOf(filenames[position]).contains(".bmp")||String.valueOf(filenames[position]).contains(".webp")||String.valueOf(filenames[position]).contains(".gif")){
thumb = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(String.valueOf(filenames[position])), 64, 64);
finalViewHolder.iv.setImageBitmap(thumb);
}
else if((String.valueOf(filenames[position])).contains(".mp4")||String.valueOf(filenames[position]).contains(".mkv")||String.valueOf(filenames[position]).contains(".webm")||String.valueOf(filenames[position]).contains(".3gp")||String.valueOf(filenames[position]).contains(".3gpp")){
thumb=ThumbnailUtils.createVideoThumbnail(filenames[position].getPath(), MediaStore.Video.Thumbnails.MINI_KIND);
finalViewHolder.iv.setImageBitmap(thumb);
}
}
};
finalViewHolder.iv.setImageResource(img);
finalViewHolder.tv.setText(myList.get(position).getName());
finalViewHolder.cb.setChecked(myList.get(position).isSelected());
t2.start();
return convertView;
}
another option I have tried is:
Making two threads, one that provide default thumbs and other same as t2 above. None works.
Upvotes: 1
Views: 59
Reputation: 5589
I suggest you look into this question which is for downloading the images from the internet into listviews. But it should be aplicable to what you are doing.
There are more things to take in mind when populating a list view with images. Yes, you run the image loading with an AsyncTask, but you also have to considera what happens when the list item goes out of site and work is still in progress. Look at the answers that have weakreferences in the asyncTask and cancel the operation if not needed anymore.
Also todya there are libraties like Ion or Picasso that do it for you.
Load asynchronous images in listView
Upvotes: 1
Reputation: 8851
private class SomeTask extends AsyncTask<URL, Integer, Long> {
protected void doInBackground(String... str) {
//Do the background stuff here
}
}
protected void onProgressUpdate(Integer... progress) {
}
protected void onPostExecute(Long result) {
//Do the UI stuff here
}
}
Upvotes: 1
Reputation: 140
Run on UI Thread. Hope this help you
runOnUiThread(new Runnable() {
@Override
public void run() {
//Your code to run in GUI thread here
}//public void run() {
});
Upvotes: 0