UniversalImageLoader崩溃(同时向上和向下滚动)

时间:2016-04-08 09:46:09

标签: java android multithreading universal-image-loader

我正在使用NOSTRAs UniversalImageLoader在gridview中崩溃。滚动列表中有大约1.5k的图像。但我猜它与内存中的位图无关,但它与太多的线程创建有关。基本上我上下滚动,上下非常狂野。我想有太多的线程创建,我经常在日志中看到这个

D/dalvikvm: create interp thread : stack size=32KB
D/dalvikvm: create new thread
D/dalvikvm: new thread created
D/dalvikvm: update thread list

最终我看到

E/dalvikvm: Thread creation failed (err=Invalid argument, stacksize=1085584KB)
A/libc: Fatal signal 7 (SIGBUS) at 0x84849142 (code=1), thread 28383 (nomad5.beatsnap)

或有时候

E/dalvikvm: Thread creation failed (err=Invalid argument, stacksize=1085584KB)
A/libc: Fatal signal 11 (SIGSEGV) at 0x84849142 (code=2), thread 14546 (nomad5.beatsnap)

有时会与

配对
java.lang.OutOfMemoryError: pthread_create (stack size 16384 bytes) failed: Try again 

但有时这个崩溃不会显示在日志中。这是我的UIL配置

.denyCacheImageMultipleSizesInMemory().diskCacheSize(64 * 1024 * 1024).threadPoolSize(5);

这里是我的显示选项

.bitmapConfig(Bitmap.Config.RGB_565)
.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
.cacheInMemory(false).cacheOnDisk(true).showImageOnFail(R.drawable.ic_image_choose_error)
.showImageForEmptyUri(R.drawable.ic_image_choose_error)

我正在加载已经非常小的图像。这是加载调用

ImageLoader.getInstance()
                   .displayImage("file://" + this.imageList.get(position),
                                 new ImageViewAware(imageView),
                                 this.options,
                                 new ImageSize(100, 100),
                                 new SimpleImageLoadingListener()
                                 {
                                     @Override
                                     public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage)
                                     {
                                     ...
                                     }
                                 },
                                 null);

顺便说一下,我的应用内存一直稳定在40MB左右。

enter image description here

因此,您看到我禁用了所有内存缓存。我也在XML中使用大堆。

有关如何避免此崩溃的任何建议吗?有没有办法限制线程创建计数?

更新#1

我已使用loadImage尝试过并删除了所有其他代码。它仍在发生。

这是我的适配器代码。 imageList是一个包含约1.5k条目的列表。只是手机上图像的路径。

public class ImageAdapter extends BaseAdapter
{
    // all the paths
    private final ArrayList<String> imageList;
    // the layout inflater
    private final LayoutInflater    layoutInflater;
    // checked array
    private final SparseBooleanArray checkedArray = new SparseBooleanArray();
    // options for loading into the grid
    private final DisplayImageOptions               options;

    /**
     * Constructor to use
     */
    public ImageAdapter(@NonNull Context context,
                        @NonNull ArrayList<String> imageList)
    {
        // init members
        this.layoutInflater = LayoutInflater.from(context);
        this.imageList = imageList;
        // image options
        this.options = new DisplayImageOptions.Builder().bitmapConfig(Bitmap.Config.RGB_565)
                                                        .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
                                                        .cacheInMemory(false)
                                                        .cacheOnDisk(true)
                                                        .showImageOnFail(R.drawable.ic_image_choose_error)
                                                        .showImageForEmptyUri(R.drawable.ic_image_choose_error)
                                                        .imageScaleType(ImageScaleType.EXACTLY)
                                                        .build();
    }  

    @Override
    public int getCount()
    {
        return this.imageList.size();
    }

    @Override
    public Object getItem(int position)
    {
        return null;
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        if(convertView == null)
        {
            convertView = this.layoutInflater.inflate(R.layout.fragment_image_choose_element, null);
        }
        convertView.setTag(position);            
        // load the image
        final View finalConvertView = convertView;
        ImageLoader.getInstance()
                   .loadImage("file://" + this.imageList.get(position),
                              new ImageSize(100, 100),
                              this.options,
                              null);           
        return convertView;
    }  
}

以下是fragment_image_choose_element.xml方法中夸大的文件getView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id = "@+id/image_choose_element"
    xmlns:android = "http://schemas.android.com/apk/res/android"
    android:layout_width = "wrap_content"
    android:layout_height = "wrap_content"
    android:layout_gravity = "center"
    android:gravity = "center">

    <ImageView
        android:id = "@+id/image_choose_element_image"
        android:layout_width = "match_parent"
        android:layout_height = "match_parent"
        android:adjustViewBounds = "false"
        android:scaleType = "centerCrop"/>    

</RelativeLayout>

0 个答案:

没有答案