使用齐射库

时间:2015-09-29 12:39:23

标签: android android-volley

我使用 ImageView 并从服务器设置图片,我使用Volley库连接服务器并获取数据。
但启动应用程序和滚动项目时,显示FC错误。我的商品有RecyclerViewCardView和自定义ImageView
我的适配器代码:

public class DataAdapter extends RecyclerView.Adapter {
    private final int VIEW_ITEM = 1;
    private final int VIEW_PROG = 0;

    private List<newSMS_class> sms_list;

    private int visibleThreshold = 5;
    private int lastVisibleItem, totalItemCount;
    private boolean loading;
    private OnLoadMoreListener onLoadMoreListener;
    private Bitmap bm;
    private Context context;

    public DataAdapter(List<newSMS_class> sms_list_use, RecyclerView recyclerView) {
        sms_list = sms_list_use;
        context = recyclerView.getContext();

        if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {

            final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView
                    .getLayoutManager();

            recyclerView
                    .addOnScrollListener(new RecyclerView.OnScrollListener() {
                        @Override
                        public void onScrolled(RecyclerView recyclerView,
                                               int dx, int dy) {
                            super.onScrolled(recyclerView, dx, dy);

                            totalItemCount = linearLayoutManager.getItemCount();
                            lastVisibleItem = linearLayoutManager
                                    .findLastVisibleItemPosition();
                            if (!loading
                                    && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
                                if (onLoadMoreListener != null) {
                                    onLoadMoreListener.onLoadMore();
                                }
                                loading = true;
                            }
                        }
                    });
        }
    }

    @Override
    public int getItemViewType(int position) {
        return sms_list.get(position) != null ? VIEW_ITEM : VIEW_PROG;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                      int viewType) {
        RecyclerView.ViewHolder vh;

        if (viewType == VIEW_ITEM) {
            View v = LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.newsms_card_layout, parent, false);

            vh = new StudentViewHolder(v);
        } else {
            View v = LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.progress_item, parent, false);

            vh = new ProgressViewHolder(v);
        }
        return vh;
    }

    private Bitmap getImg(String url) {
        RequestQueue queue = Volley.newRequestQueue(context);

        ImageRequest ir = new ImageRequest("http://smsbox.tellfa.com/UploadUserImage/Cover/img_" + url + ".jpg", new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                bm = response;
            }
        }, 0, 0, null, null);
        queue.add(ir);
        //ir.setShouldCache(false);
        return bm;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof StudentViewHolder) {

            newSMS_class singleStudent = sms_list.get(position);

            ((StudentViewHolder) holder).sms_content_new.setText(singleStudent.getSms());
            ((StudentViewHolder) holder).sms_username.setText(singleStudent.getUsername());
            ((StudentViewHolder) holder).sms_count.setText(singleStudent.getCountsms());
            ((StudentViewHolder) holder).sms_category.setText(singleStudent.getCategory());
            ((StudentViewHolder) holder).sms_category.setTag(singleStudent.getCat_id());
            ((StudentViewHolder) holder).sms_date.setText(singleStudent.getDate());
            ((StudentViewHolder) holder).sms_hour.setText(singleStudent.getHour());
            Bitmap b = getImg(singleStudent.getAvatar());
            ((StudentViewHolder) holder).sms_avatar.setImageBitmap(b);

            ((StudentViewHolder) holder).sms_class = singleStudent;
        } else {
            ((ProgressViewHolder) holder).progressBar.setIndeterminate(true);
        }
    }

    public void setLoaded() {
        loading = false;
    }

    @Override
    public int getItemCount() {
        return sms_list.size();
    }

    public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
        this.onLoadMoreListener = onLoadMoreListener;
    }


    //
    public static class StudentViewHolder extends RecyclerView.ViewHolder {
        public TextView sms_content_new, sms_username, sms_count, sms_category, sms_date, sms_hour;
        public ImageView sms_avatar;

        public newSMS_class sms_class;

        public StudentViewHolder(View v) {
            super(v);
            sms_content_new = (TextView) v.findViewById(R.id.sms_newsms_text);
            sms_username = (TextView) v.findViewById(R.id.newSMS_username_text);
            sms_count = (TextView) v.findViewById(R.id.newSMS_count_text);
            sms_category = (TextView) v.findViewById(R.id.newSMS_category_text);
            sms_date = (TextView) v.findViewById(R.id.newSMS_Date_text);
            sms_hour = (TextView) v.findViewById(R.id.newSMS_hour_text);

            sms_avatar = (ImageView) v.findViewById(R.id.users_avatar);

            sms_content_new.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(final View v) {

                    final Dialog showSMS_dialog = new Dialog(v.getContext());
                    showSMS_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    showSMS_dialog.setContentView(R.layout.show_sms__page);
                    showSMS_dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
                    Window window = showSMS_dialog.getWindow();
                    window.setLayout(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                    showSMS_dialog.setCancelable(false);
                    showSMS_dialog.show();

                    ImageView closeDialog_image = (ImageView) showSMS_dialog.findViewById(R.id.addSMS_close_image);
                    TextView sms_txt = (TextView) showSMS_dialog.findViewById(R.id.addSMS_smscontent_text);
                    ImageView avatar = (ImageView) showSMS_dialog.findViewById(R.id.addSMS_avatar_image);
                    ImageView copy = (ImageView) showSMS_dialog.findViewById(R.id.addSMS_toolBar_copy);
                    ImageView share = (ImageView) showSMS_dialog.findViewById(R.id.addSMS_toolBar_share);
                    avatar.setImageDrawable(sms_avatar.getDrawable());
                    sms_txt.setText(sms_content_new.getText());

                    share.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent2 = new Intent();
                            intent2.setAction(Intent.ACTION_SEND);
                            intent2.setType("text/plain");
                            intent2.putExtra(Intent.EXTRA_TEXT, sms_content_new.getText());
                            v.getContext().startActivity(Intent.createChooser(intent2, "به یکی بفرست ;)"));
                        }

                    });
                    copy.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            ClipboardManager clipboard = (ClipboardManager) v.getContext().getSystemService(v.getContext().CLIPBOARD_SERVICE);
                            clipboard.setText(sms_content_new.getText());
                            Toast.makeText(v.getContext(), "کپی شد مشتیییی :))", Toast.LENGTH_SHORT).show();
                        }

                    });

                    closeDialog_image.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            showSMS_dialog.dismiss();
                        }
                    });
                }
            });
        }
    }

    public static class ProgressViewHolder extends RecyclerView.ViewHolder {
        public ProgressBar progressBar;

        public ProgressViewHolder(View v) {
            super(v);
            progressBar = (ProgressBar) v.findViewById(R.id.progressBar1);
        }
    }
}

Logcat代码:

09-29 15:57:56.223  13523-13602/com.tellfa.smsbox E/Adreno-GSL﹕ <gsl_memory_alloc_pure:2044>: GSL MEM ERROR: kgsl_sharedmem_alloc ioctl failed.
09-29 15:57:56.233  13523-13602/com.tellfa.smsbox W/Adreno-GSL﹕ <sharedmem_gpumem_alloc_id:1498>: sharedmem_gpumem_alloc: mmap failed errno 12 Out of memory
09-29 15:57:56.233  13523-13602/com.tellfa.smsbox E/Adreno-GSL﹕ <gsl_memory_alloc_pure:2044>: GSL MEM ERROR: kgsl_sharedmem_alloc ioctl failed.
09-29 15:57:56.236  13523-13602/com.tellfa.smsbox W/Adreno-ES20﹕ <core_glTexImage2D:539>: GL_OUT_OF_MEMORY
09-29 15:57:56.277  13523-13602/com.tellfa.smsbox E/OpenGLRenderer﹕ GL error:  Out of memory!
09-29 15:57:56.288  13523-13523/com.tellfa.smsbox W/libc﹕ pthread_create failed: couldn't allocate 1064960-byte stack: Out of memory
09-29 15:57:56.289  13523-13523/com.tellfa.smsbox E/art﹕ Throwing OutOfMemoryError "pthread_create (1040KB stack) failed: Try again"
09-29 15:57:56.289  13523-13523/com.tellfa.smsbox D/AndroidRuntime﹕ Shutting down VM
09-29 15:57:56.319  13523-13523/com.tellfa.smsbox E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.tellfa.smsbox, PID: 13523
    java.lang.OutOfMemoryError: pthread_create (1040KB stack) failed: Try again
            at java.lang.Thread.nativeCreate(Native Method)
            at java.lang.Thread.start(Thread.java:1063)
            at com.android.volley.RequestQueue.start(RequestQueue.java:134)
            at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:66)
            at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:78)
            at com.tellfa.smsbox.adapters.DataAdapter.getImg(DataAdapter.java:104)
            at com.tellfa.smsbox.adapters.DataAdapter.onBindViewHolder(DataAdapter.java:130)
            at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5138)
            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4433)
            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4326)
            at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1955)
            at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1364)
            at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1327)
            at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1155)
            at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1012)
            at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:3777)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
            at android.view.Choreographer.doCallbacks(Choreographer.java:580)
            at android.view.Choreographer.doFrame(Choreographer.java:549)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5349)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)

请帮我修理一下。大tnx&lt; 3

2 个答案:

答案 0 :(得分:2)

正如@Selvin所说,你不应该为每个图像创建新的请求。 为此,您需要从“Bitmap getImg(String url)”方法中删除以下行。

"RequestQueue queue = Volley.newRequestQueue(context);"

例如,将变量声明放在某处,如下面的Context声明之后:

...

private Bitmap bm;
private Context context;
RequestQueue queue = Volley.newRequestQueue(context);

...

答案 1 :(得分:2)

从每个工作活动中发起新的requestQueue实际上并不是一个好主意。

相反,我创建了一个类似于以下的应用程序类 -

public class ApplicationController extends Application{
    private static ApplicationController thisInstance ;
    private RequestQueue mRequestQueue ;

@Override
public void onCreate() {
    super.onCreate();
    thisInstance = this ;
}

public static synchronized ApplicationController getInstance(){
    return thisInstance ;
}

public RequestQueue getmRequestQueue(){
    if (mRequestQueue == null) {
        mRequestQueue = Volley.newRequestQueue(getApplicationContext());
    }

    return mRequestQueue;
}

无论我在哪里初始化,我都用

requestQueue = ApplicationController.getInstance().getmRequestQueue();

您只需将其添加到您的Manifest应用程序元素的名称属性中,您可以在其中提供您的班级名称。

 <application
    android:name=".ApplicationController"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:theme="@style/AppTheme">

但我认为您现在只拥有整个应用的一个RequestQueue 实例。如果您认为可能会产生大量的HttpRequests,那么您可以根据需要在某些活动中实例化RequestQueues。这对我帮助很大。我希望它也可以帮到你。