Issue
is it possible to load the Thumbnails of a GridView in an Asynctask, to prevent lags on scrolling the GridView?
That's the code of my Asynctask:
@SuppressLint("StaticFieldLeak")
public class AsyncTaskLoadFiles2 extends AsyncTask<Void, String, Void> {
File targetDirector;
public AsyncTaskLoadFiles2(ImageAdapter2 adapter) {
myTaskAdapter2 = adapter;
}
@Override
protected void onPreExecute() {
String targetPath = "/sdcard/Android/data/de.myapps.gridtest/files/Download/.Videos";
targetDirector = new File(targetPath);
myTaskAdapter2.clear2();
progressDialog = new ProgressDialog(getContext());
progressDialog.setMessage("Loading Videos, please wait...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setCancelable(false);
progressDialog.show();
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
File[] files = targetDirector.listFiles();
for (File file : files) {
publishProgress(file.getAbsolutePath());
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (isCancelled()) break;
}
return null;
}
@Override
protected void onProgressUpdate(String... values) {
myTaskAdapter2.add2(values[0]);
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Void result) {
myTaskAdapter2.notifyDataSetChanged();
progressDialog.dismiss();
super.onPostExecute(result);
}
}
And here's the code of my ImageAdapter:
public class ImageAdapter2 extends BaseAdapter {
private Context mContext;
ArrayList<String> itemList = new ArrayList<String>();
public ImageAdapter2(Context c) {
mContext = c;
}
void add2(String path) {
itemList.add(path);
}
void clear2() {
itemList.clear();
}
void remove2(int index) {
itemList.remove(index);
}
public String getPath(int position) {
return itemList.get(position);
}
@Override
public int getCount() {
return itemList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return itemList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@SuppressLint("ResourceType")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 420));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
} else {
imageView = (ImageView) convertView;
}
thumbnail = ThumbnailUtils.createVideoThumbnail(getPath(position),
MediaStore.Images.Thumbnails.MICRO_KIND);
// Set the decoded bitmap into ImageView
imageView.setImageBitmap(thumbnail);
return imageView;
}
}
So what I would like to know is if it is possible to load the thumbnail
Bitmap in the Asynctask, because with this code the app lags. (I'm using a TabbedActivity so all this code is part of a Fragment)
Solution
Have a look at
https://developer.android.com/topic/performance/graphics/cache-bitmap
BitmapWorkerTask task = new BitmapWorkerTask(mImageView);
task.execute(resId);
You can create an AsyncTask that loads the image into the ImageView
class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
...
// Decode image in background.
@Override
protected Bitmap doInBackground(Integer... params) {
final Bitmap bitmap = decodeSampledBitmapFromResource(getResources(), params[0], 100, 100));
return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
if(result != null) {
myImgView.setImageBitmap(result);
}
}
}
You just call BitmapWorkerTask.Execute
with parameters in the constructor from the getView()
method in your adapter.
BitmapWorkerTask task = new BitmapWorkerTask(imageView, filePath);
Make the constructor
class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
private ImageView imageView;
private String filePath;
public BitmapWorkerTask(ImageView imageView, String filePath) {
this.imageView = imageView;
this.filePath = filePath;
}
....
//Get your bitmap
//Set Bitmap onPostExecute
Answered By - Pierre
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.