在我的代码中,我在我的ImageView
类中使用MyTask
将照片设置为AsyncTask
,但是我运行的代码只有一两个{ {1}} s有图片,其他是空的,显示默认图片。
线程有什么问题吗?
这是我的代码:
ImageView
答案 0 :(得分:0)
我认为您尝试使用网址加载图片
您可以使用最适合加载图像的图像加载器参考(例如通用图像加载器)
这是您可以拥有的代码
首先从下面下载通用图像加载器库 https://github.com/nostra13/Android-Universal-Image-Loader
您必须将此方法添加到您的应用程序级别,这样您就不需要多次编写
public static void initImageLoader(Context context) {
// This configuration tuning is custom. You can tune every option, you
// may tune some of them,
// or you can create default configuration by
// ImageLoaderConfiguration.createDefault(this);
// method.
ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(
context);
config.threadPriority(Thread.NORM_PRIORITY - 2);
config.denyCacheImageMultipleSizesInMemory();
config.diskCacheFileNameGenerator(new Md5FileNameGenerator());
config.diskCacheSize(50 * 1024 * 1024); // 50 MiB
config.tasksProcessingOrder(QueueProcessingType.LIFO);
config.writeDebugLogs(); // Remove for release app
// Initialize ImageLoader with configuration.
ImageLoader.getInstance().init(config.build());
}
然后在Adapter类中,您只需添加以下代码来加载图像
// For Image Display options - Failed , Success and Error
// Add this is in your adapter constructor
DisplayImageOptions userimgoptions = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.profile_pic_no_border)
.showImageForEmptyUri(R.drawable.profile_pic_no_border)
.showImageOnFail(R.drawable.profile_pic_no_border).cacheInMemory(true)
.cacheOnDisk(true).bitmapConfig(Bitmap.Config.RGB_565).build();
//在你的getview方法中只需使用此符号行代码加载图片
ImageLoader.getInstance().displayImage(modelFeeds.getUserAvatarOriginal(), myHolder.img_profilepicture, userimgoptions);
我希望这会对你有所帮助
答案 1 :(得分:0)
使用Glide Library设置Imageview
Step1 :在build.gradle文件中包含依赖项
compile 'com.github.bumptech.glide:glide:3.7.0'
Step2 :在Java文件中包含Glide
String fileName = "1.jpg";
String completePath = Environment.getExternalStorageDirectory() + "/" +
fileName;
File file = new File(completePath);
Uri imageUri = Uri.fromFile(file);
Glide.with(this)
.load(imageUri)
.placeholder(R.drawable.no_media)
.into(imgView);
答案 2 :(得分:0)
这是因为每次调用getView()方法时都会覆盖iv(imageview)对象,因为MyTask是一个异步任务,在后台线程上异步调用doInBackground(),在主线程上调用onPostExecute()之后。因此onPostExecute只会更新当前的imageview。您必须在网格的每个图像视图上设置位图。此解决方案应该适合您:
@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);
ImageView iv = (ImageView) convertView.findViewById(R.id.icon);
File file = new File(Uri.parse(getItem(position).toString()).getPath());
new myTask(iv, file).execute();
return convertView;
}
private class MyTask extends AsyncTask <Void , Void ,Bitmap> {
Imageview iv;
File file;
public void MyTask(Imageview iv, File file)
{
this.iv=iv;
this.file= file;
}
@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);
}
}