【发布时间】:2017-08-06 08:22:53
【问题描述】:
在我的代码中,我在我的MyTask 类中将图片设置为ImageView,该类使用AsyncTask,但是当我运行我的代码时,只有一两个ImageViews 有图片,而其他图片为空,显示默认图片。
线程有问题吗?
这是我的代码:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Log.e("sara" , "this part takes time");
LayoutInflater inflater = getLayoutInflater();
convertView = getLayoutInflater().inflate(R.layout.gallery_gridsq, parent, false);
iv = (ImageView) convertView.findViewById(R.id.icon);
file = new File(Uri.parse(getItem(position).toString()).getPath());
new myTask().execute();
return convertView;
}
private class myTask extends AsyncTask <Void , Void ,Bitmap> {
@Override
protected Bitmap doInBackground(Void... params) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
try {
BitmapFactory.decodeStream(new FileInputStream(file), null, options);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
options.inJustDecodeBounds = false;
options.inSampleSize = 2;
try {
bmp = BitmapFactory.decodeStream(new FileInputStream(file), null, options);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return bmp;
}
@Override
protected void onPostExecute(Bitmap aVoid) {
iv.setImageBitmap(aVoid);
}
}
【问题讨论】:
-
您的日志中是否有任何错误..?
-
要不要在 asyncTask.不需要使用位图。你可以简单地使用滑动方法。
标签: java android multithreading android-asynctask