gpt4 book ai didi

android - 下载图像和更新 listView 的问题

转载 作者:太空宇宙 更新时间:2023-11-03 12:30:06 24 4
gpt4 key购买 nike

我在下载图像和更新 ImageView 时遇到问题,我正在使用 ExecutorServices 下载图像,但我面临的问题是,就场景而言,我正在使用基本适配器在列表中显示 Imageview。图像被下载但是这两个图像仅在 firstImage View 中更新。

所以位图在同一个imageView中得到更新,有没有人遇到过类似的问题

例如,我正在下载 2 个图像,它正在创建 2 个 ImageDownloader 实例,所有运行良好,直到下载前部分中该类的 Run() 行,一旦图像被下载,我将获得 ImageView 的对象引用第一个 Image Downloader 类,我不知道为什么?我认为它与 listview 或适配器无关,它与 Executor 和 Runnable 实现更相关,

公共(public)类 ImageLoader {

ExecutorService mExecutorService = null;
static ImageLoader mLoader = null;
Handler mImageHandler = null ;
DisplayMetrics mDisplayMetrics = null;
int mRequiredWidth =0 , mRequiredHeight = 0;
Map<String,ImageView> mToLoadObjects = Collections.synchronizedMap(
new WeakHashMap<String, ImageView>());
ArrayList<String> toloadURL = new ArrayList<String>();
protected Resources mResources = null;
MyImageCache mMemoryCache;
Activity activity;
public static final String LOGGER = "ImageLoader";

public ImageLoader(Activity context){
mExecutorService = Executors.newFixedThreadPool(5);
mImageHandler = new Handler();
mResources = context.getResources();
activity = context;
mMemoryCache = new MyImageCache();
mDisplayMetrics = context.getResources().getDisplayMetrics();
mRequiredWidth = mDisplayMetrics.widthPixels;
mRequiredHeight = mDisplayMetrics.heightPixels;

}



public void loadImage (String url,ImageView imageView){
try {
if (imageView != null) {
if (!toloadURL.contains(url)) {
toloadURL.add(url);
mExecutorService.execute(new ImageDownloader(url,imageView));
}
}
}catch (Exception e){
Log.v("Exception Occurs>>",""+e.getMessage());
}
}




class ImageDownloader implements Runnable{
String imageUrl;
ImageView downloadableImageView;
ImageDownloader loader;


ImageDownloader(String url,ImageView view){
Log.v(LOGGER,"Within ImageDownloader "+url +"this>>>"+this);
loader = this;
imageUrl = url;
downloadableImageView = view;
Log.v(LOGGER,"Within ImageDownloader "+downloadableImageView.getTag()+"this>>>"+this);
}

@Override
public void run() {
Log.v(LOGGER, "run" + downloadableImageView.getTag()+"this>>>>>"+this);
Bitmap tempBitmap = null;
tempBitmap = (mMemoryCache.getBitmapFromMemCache(imageUrl));
try{
if(tempBitmap!=null){
Log.v(LOGGER,"ImageBitmap Wid>>>>>>"+downloadableImageView.getTag()+"this>>>>"+this+""+tempBitmap.getWidth());
Log.v(LOGGER,"ImageBitmap Ht>>>>>>"+downloadableImageView.getTag()+ "this>>>>"+this+""+tempBitmap.getHeight());
downloadableImageView.setImageBitmap(tempBitmap);
}else{
Log.v(LOGGER, "else to download Tag is" + downloadableImageView.getTag()+"this>>>>>"+this);//Works till this point

final Bitmap tempBitmap1 = getBitmap(imageUrl);
Log.v(LOGGER, "else to download Tag is " + downloadableImageView.getTag() + "After Download>>>" + tempBitmap1.getWidth() + "this>>>>>" + this);//Issue happens here
mMemoryCache.addBitmapToMemoryCache(imageUrl, tempBitmap1);
Log.v("LOGGER, else to download Tag is "+downloadableImageView.getTag()+"After Cache>>>", "" + downloadableImageView.getTag()+"this>>>>"+this);
if(tempBitmap1!=null){
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Log.v("LOGGER, else to download Tag is "+downloadableImageView.getTag()+"RunOnUIThread>>>", "" + downloadableImageView.getTag()+"this>>>>"+loader);
downloadableImageView.setImageBitmap(tempBitmap1);
downloadableImageView.requestLayout();
}
});
}
}
}catch(Exception e){
Log.v("ExceptionBitmap",""+e.getMessage());
}

}

public Bitmap getBitmap(String imageUrl){
Bitmap bitmap=null;
try {
Log.v("Within getBitmap ","run");
URL uri = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection)uri.openConnection();
connection.setInstanceFollowRedirects(true);
//bitmap = BitmapFactory.decodeStream(connection.getInputStream());
bitmap = decodeSampledBitmapFromInputStream(connection.getInputStream(), mRequiredWidth, mRequiredHeight);

} catch (Exception e) {
Log.v("ExceptionLoad",""+e.getMessage());
}
return bitmap;
}
}

public Bitmap decodeSampledBitmapFromInputStream(InputStream in
, int reqWidth, int reqHeight) {

InputStream copyiInputStream1 = null;
InputStream copyiInputStream2 = null;
try {
byte[] data = InputStreamTOByte(in);
copyiInputStream1 = byteTOInputStream(data);
copyiInputStream2 = byteTOInputStream(data);
} catch (Exception e) {
e.printStackTrace();
}
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(copyiInputStream1, null, options);

// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeStream(copyiInputStream2, null, options);
}

public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

final int halfHeight = height / 2;
final int halfWidth = width / 2;

// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}

return inSampleSize;
}

public InputStream byteTOInputStream(byte[] in) throws Exception{

ByteArrayInputStream is = new ByteArrayInputStream(in);
return is;
}

public byte[] InputStreamTOByte(InputStream in) throws IOException {

ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] data = new byte[1024*16];
int count = -1;
while((count = in.read(data,0,1024*16)) != -1)
outStream.write(data, 0, count);

data = null;
return outStream.toByteArray();
}


class BitmapUpdater implements Runnable
{
Bitmap bitmap;
ImageView imageView;

public BitmapUpdater(Bitmap bitmap, ImageView imageView){
bitmap=bitmap;
imageView =imageView;
}
public void run()
{
// Show bitmap on UI
if(bitmap!=null){
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
imageView.setImageBitmap(bitmap);
}
});
}
else
imageView.setImageResource(R.drawable.account_photo_book_detail2);
}
}

}

我已附上日志以供引用,请帮助解决此问题
Image Tag(0,1)--> I applied Tag for ImageView to sort out the Image View instance

线程 1:(ImageDownloader1 线程对象)

04-06 17:05:57.662 20053-20053/com.xxx.xxxxmobileapp V/ImageLoader:在 ImageDownloader https://lh6.googleusercontent.com/-8HO-4vIFnlw/URquZnsFgtI/AAAAAAAAAbs/WT8jViTF7vw/s1024/Antelope%252520Hallway.jpgthis 内>>>com.xxx.xxxxmobileapp.Imagebook.network.ImageLoader$ImageDownloader@435058b0
04-06 17:05:57.662 20053-20053/com.xxx.xxxxmobileapp V/ImageLoader﹕在 ImageDownloader 0(imageViewObjectTag)this>>>com.xxx.xxxxmobileapp.Imagebook.network.ImageLoader$ImageDownloader@

04-06 17:05:57.664 20053-20130/com.xxx.xxxxmobileapp V/ImageLoader: else to download Tag is0(imageViewObjectTag)this>>>>>com.xxx.xxxxmobileapp.Imagebook.network.ImageLoader$ImageDownloader@435058b0

线程 2:((ImageDownloader2 线程对象))

04-06 17:05:57.666 20053-20053/com.xxx.xxxxmobileapp V/ImageLoader:在 ImageDownloader https://lh5.googleusercontent.com/-7qZeDtRKFKc/URquWZT1gOI/AAAAAAAAAbs/hqWgteyNXsg/s1024/Another%252520Rockaway%252520Sunset.jpgthis 内>>>com.xxx.xxxxmobileapp.Imagebook.network.ImageLoader$ImageDownloader@431a5d58
04-06 17:05:57.666 20053-20053/com.xxx.xxxxmobileapp V/ImageLoader﹕在 ImageDownloader 1(imageView)this>>>com.xxx.xxxxmobileapp.Imagebook.network.ImageLoader$ImageDownloader@431a5d58

线程 1 & 2 运行 ():

20053-20130/com.xxx.xxxxmobileapp V/ImageLoader﹕运行 0(imageViewObjectTag)this>>>>>com.xxx.xxxxmobileapp.Imagebook.network.ImageLoader$ImageDownloader@435058b0
20053-20131/com.xxx.xxxxmobileapp V/ImageLoader:运行 1(imageViewObjectTag)this>>>>>com.xxx.xxxxmobileapp.Imagebook.network.ImageLoader$ImageDownloader@431a5d58

else to download Tag is1this>>>>>com.xxx.xxxxmobileapp.Imagebook.network.ImageLoader$ImageDownloader@431a5d58

((ImageDownloader1 Thread Object)) 下载后:

else to download Tag is 0(imageViewObjectTag)After Download>>>1024this>>>>>com.xxx.xxxxmobileapp.Imagebook.network.ImageLoader$ImageDownloader@435058b0//(ImageDownloader1 Object))
else to download Tag is 0(imageViewObjectTag)After Cache>>>: 0this>>>>com.xxx.xxxxmobileapp.Imagebook.network.ImageLoader$ImageDownloader@435058b0//(ImageDownloader1 Object))
else to download Tag is 0(imageViewObjectTag)RunOnUIThread>>>: 0this>>>>com.xxx.xxxxmobileapp.Imagebook.network.ImageLoader$ImageDownloader@435058b0//(ImageDownloader1 Object))

一切都好,直到这一点

((ImageDownloader2 Thread Object))下载后:

else to download Tag is 0(imageViewObjectTag)After Download>>>1024this>>>>>com.xxx.xxxxmobileapp.Imagebook.network.ImageLoader$ImageDownloader@431a5d58//(ImageDownloader2 Object with ImageView tag来作为0不同应该是标签1))
else to download Tag is 0(imageViewObjectTag)After Cache>>>: 0this>>>>com.xxx.xxxxmobileapp.Imagebook.network.ImageLoader$ImageDownloader@431a5d58//(ImageDownloader2 Object with ImageView tag come as 0 different which should be标签1))
else to download Tag is 0(imageViewObjectTag)RunOnUIThread>>>: 0this>>>>com.xxx.xxxxmobileapp.Imagebook.network.ImageLoader$ImageDownloader@431a5d58//(ImageDownloader2 Object with ImageView tag 作为0不同应该是tag1 ))

最佳答案

为什么不使用 Glide 库进行图像下载?

它在一行中完成所有事情。

这是它的语法

Glide.with(context).load(URL).into(Imageview);

关于android - 下载图像和更新 listView 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36440770/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com