问题
I have another issue with Universal Image loader. Trying to use it in a listview and gridview for displaying large amount of images. The urls passed to UIL contain high res images. I have tried using almost all scaletypes, and it still loads images much slower on mobile network than Original LazyList. As I understand it first loads the exact image and then resizes it. Is there a way to download smaller amount of data to display this very much scaled image. Also it sometimes craches while loading images with two different exceptions. EOFException, and SocketTimeoutException. Any ideas what I am doing wrong here ?
Update: Adapter class:
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private List<String[]> data;
private static LayoutInflater inflater=null;
public ImageLoade1r imageLoader;
public boolean show;
public final String webs = "http://www";
public DisplayImageOptions options;
public ImageLoader nostra;
public LazyAdapter(Activity a, List<String[]> d, boolean flag) {
activity = a;
data=d;
show=flag;
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(a.getApplicationContext())
.enableLogging()
.build();
nostra = ImageLoader.getInstance();
nostra.init(config);
options = new DisplayImageOptions.Builder()
.cacheOnDisc()
.showImageOnFail(R.drawable.failed)
.imageScaleType(ImageScaleType.EXACTLY)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoade1r(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
String[] url = data.get(position);
if(convertView==null)
vi = inflater.inflate(R.layout.item, null);
TextView text=(TextView)vi.findViewById(R.id.text);;
ImageView image=(ImageView)vi.findViewById(R.id.image);
ProgressBar spinner = (ProgressBar) vi.findViewById(R.id.prgrsItem);
RelativeLayout layo = (RelativeLayout) vi.findViewById(R.id.thelayout);
if(url[1].trim().equals("folder"))
{
String img = webs+url[3].trim() + url[2] +"title"+".jpg";
display(image, img, spinner);
text.setText(url[2].replaceAll("_", " "));
}
else
{
String img;
if(show==true)
img = webs+url[3] + url[2]+"title"+".jpg";
else
img = webs+url[0];
display(image, img, spinner);
text.setText(url[2].replaceAll("_", " "));
}
if(show==true)
{
text.setVisibility(View.VISIBLE);
}
else
{
text.setVisibility(View.GONE);
}
return vi;
}
public void display(ImageView img, String url, final ProgressBar spinner)
{
nostra.displayImage(url, img, options, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
spinner.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
spinner.setVisibility(View.GONE);
EasyTracker.getTracker().sendException(failReason.toString(), false);
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
spinner.setVisibility(View.GONE);
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
}
});
}
}
Update EOFException:
03-21 13:51:55.800: E/ImageLoader(31605): null
03-21 13:51:55.800: E/ImageLoader(31605): java.io.EOFException
03-21 13:51:55.800: E/ImageLoader(31605): at libcore.io.Streams.readAsciiLine(Streams.java:203)
03-21 13:51:55.800: E/ImageLoader(31605): at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:544)
03-21 13:51:55.800: E/ImageLoader(31605): at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:784)
03-21 13:51:55.800: E/ImageLoader(31605): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
03-21 13:51:55.800: E/ImageLoader(31605): at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:479)
03-21 13:51:55.800: E/ImageLoader(31605): at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStreamFromNetwork(BaseImageDownloader.java:113)
03-21 13:51:55.800: E/ImageLoader(31605): at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStream(BaseImageDownloader.java:84)
03-21 13:51:55.800: E/ImageLoader(31605): at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.saveImageOnDisc(LoadAndDisplayImageTask.java:299)
03-21 13:51:55.800: E/ImageLoader(31605): at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:237)
03-21 13:51:55.800: E/ImageLoader(31605): at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:149)
03-21 13:51:55.800: E/ImageLoader(31605): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-21 13:51:55.800: E/ImageLoader(31605): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-21 13:51:55.800: E/ImageLoader(31605): at java.lang.Thread.run(Thread.java:856)
回答1:
Use should use a view holder for performance.
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
Your code might call findViewById() frequently during the scrolling of ListView, which can slow down performance. Even when the Adapter returns an inflated view for recycling, you still need to look up the elements and update them. A way around repeated use of findViewById() is to use the "view holder" design pattern.
http://www.youtube.com/watch?v=wDBM6wVEO70. The topic is abot listview and view holder.
SokectTimeOut Exception is thrown when a timeout expired on a socket read or accept operation.
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(a.getApplicationContext())
.enableLogging()
.build();
.imageDownloader(new URLConnectionImageDownloader(5 * 1000, 20 * 1000)) // connectTimeout (5 s), readTimeout (20 s)
For EOFException check the link.
http://code.google.com/p/libs-for-android/issues/detail?id=14.
System.setProperty("http.keepAlive", "false").
Issue similar was raised and some suggested to try the above before opening HTTP url connection.
Probably has nothing to do with your code.
来源:https://stackoverflow.com/questions/15543811/uil-loading-exact-image