【问题标题】:How can I load images in GridViews after a while with no error?如何在没有错误的情况下在 GridViews 中加载图像?
【发布时间】:2015-05-07 08:31:39
【问题描述】:

我正在尝试将我的应用程序的 res->drawable 文件夹中的图像加载到 12 GridViews 中(对于每个 GridView 几乎 15 个图像)。

我可以将图像加载到 GridViews 的 7 个中,但之后会出现 outofmemory error。我调整了图像大小,使用此选项我可以在 7 GridView 中加载图像。

由于我不需要加载所有图像,我尝试使用处理程序类加载一些图像,延迟 15 秒,但仍然出现 outofmemory 错误。

我使用了System.gc(),在使用了图像数组之后,我将它们全部设为空。

但仍然出现outofmemory 错误。我认为对我有帮助的一件事是当用户向下滚动到某个位置时将图像加载到GridViews。乙

但是ScrollView 类没有用于此的方法,并且有一个受保护的OnScrollmethod,我认为我不能使用它。

这是在GridView1 中加载图像的代码。所有的代码都是这样的。

 try {
    final GridviewAdapter mAdapter1;    
    final GridView  gridView1 = (GridView)findViewById(R.id.gridView1);


            ArrayList<String> listCountry1;
            ArrayList<Bitmap> listFlag1;

              listCountry1 = new ArrayList<String>();
              listFlag1 = new ArrayList<Bitmap>();



              Bitmap unscaledBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.w1);

              Bitmap scaledBitmap = Bitmap.createScaledBitmap(unscaledBitmap, dstwidth, dstheight, true);
              Bitmap unscaledBitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.w12);

              Bitmap scaledBitmap2 = Bitmap.createScaledBitmap(unscaledBitmap2, dstwidth, dstheight, true);
              Bitmap unscaledBitmap3 = BitmapFactory.decodeResource(getResources(), R.drawable.w15);

              Bitmap scaledBitmap3 = Bitmap.createScaledBitmap(unscaledBitmap3, dstwidth, dstheight, true);
              Bitmap unscaledBitmap4 = BitmapFactory.decodeResource(getResources(), R.drawable.w26);

              Bitmap scaledBitmap4 = Bitmap.createScaledBitmap(unscaledBitmap4, dstwidth, dstheight, true);
              Bitmap unscaledBitmap5 = BitmapFactory.decodeResource(getResources(), R.drawable.w28);

              Bitmap scaledBitmap5 = Bitmap.createScaledBitmap(unscaledBitmap5, dstwidth, dstheight, true);
              Bitmap unscaledBitmap6 = BitmapFactory.decodeResource(getResources(), R.drawable.w33);

              Bitmap scaledBitmap6 = Bitmap.createScaledBitmap(unscaledBitmap6, dstwidth, dstheight, true);
              Bitmap unscaledBitmap7 = BitmapFactory.decodeResource(getResources(), R.drawable.w44);

              Bitmap scaledBitmap7 = Bitmap.createScaledBitmap(unscaledBitmap7, dstwidth, dstheight, true);
              Bitmap unscaledBitmap8 = BitmapFactory.decodeResource(getResources(), R.drawable.w46);

              Bitmap scaledBitmap8 = Bitmap.createScaledBitmap(unscaledBitmap8, dstwidth, dstheight, true);
              Bitmap unscaledBitmap9 = BitmapFactory.decodeResource(getResources(), R.drawable.w50);

              Bitmap scaledBitmap9 = Bitmap.createScaledBitmap(unscaledBitmap9, dstwidth, dstheight, true);




                                           listCountry1.add("book0");
                                           listCountry1.add("book1");
                                           listCountry1.add("book2");
                                           listCountry1.add("book3");
                                           listCountry1.add("book4");
                                           listCountry1.add("book5");
                                           listCountry1.add("book6");
                                           listCountry1.add("book7");
                                           listCountry1.add("book8");
                                           //
                                           listFlag1.add(scaledBitmap);
                                           listFlag1.add(scaledBitmap2);
                                           listFlag1.add(scaledBitmap3);
                                           listFlag1.add(scaledBitmap4);
                                           listFlag1.add(scaledBitmap5);
                                           listFlag1.add(scaledBitmap6);
                                           listFlag1.add(scaledBitmap7);
                                           listFlag1.add(scaledBitmap8);
                                           listFlag1.add(scaledBitmap9);

                                               mAdapter1 = new GridviewAdapter(FirstPageActivity.this,listCountry1, listFlag1);


                                                gridView1.setAdapter(mAdapter1);







    // Implement On Item click listener
                               gridView1.setOnItemClickListener(new OnItemClickListener() 
    {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
            Toast.makeText(FirstPageActivity.this, mAdapter1.getItem(position), Toast.LENGTH_SHORT).show();
        }
    });






                               gridView1.getViewTreeObserver().addOnGlobalLayoutListener( new OnGlobalLayoutListener() 
                        {
                        @Override
                        public void onGlobalLayout() 
                            {
                            gridView1.getViewTreeObserver().removeGlobalOnLayoutListener( this );
                            View lastChild = gridView1.getChildAt( gridView1.getChildCount() - 1 );
                            gridView1.setLayoutParams( new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, lastChild.getBottom() ) );
                            }
                        });

                               imageLoader1.clearDiscCache();
                              imageLoader1.clearDiskCache();
                              imageLoader1.clearMemoryCache();


                               unscaledBitmap=null;scaledBitmap=null;
                               unscaledBitmap2=null;scaledBitmap2=null;
                               unscaledBitmap3=null;scaledBitmap3=null;
                               unscaledBitmap4=null;scaledBitmap4=null;
                               unscaledBitmap5=null;scaledBitmap5=null;
                               unscaledBitmap6=null;scaledBitmap6=null;
                               unscaledBitmap7=null;scaledBitmap7=null;
                               unscaledBitmap8=null;scaledBitmap8=null;
                               unscaledBitmap9=null;scaledBitmap9=null;


                                  listFlag1=null;
                                  listCountry1=null;
                                  java.lang.System.gc();


    }
    catch (Exception e) {
        // TODO Auto-generated catch block
        Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
    }

【问题讨论】:

  • 能否请您发布您的代码,以加载图像?你如何储存它们?

标签: android image gridview


【解决方案1】:

-首先:您需要将项目的堆大小设置为更多(10 - 50 MB),这有助于您的应用程序 - 避免内存不足。 - 第二:您应该将原始图像的大小调整为拇指图像大小(例如 70x70 像素),这有助于位图解码器加载最小的图像以显示在网格上

所以,你可以在这里展示你的代码,我可以支持更多...

【讨论】:

  • 我在清单中使用了 large_heap="true" 和 hardware_acceleration="true",我将它们调整为 120*120,当我想在 gridview8 中加载图像时仍然出现内存不足错误。当我发生此错误时想要在超过 7 个网格视图中加载图像,并且不是来自 gridview8。关于我的代码:它非常大而且非常混乱,但我在一个 gridview 中添加了一个加载部分代码。
【解决方案2】:

使用一些图像加载库,例如 Picasso 或 Glide。您可以使用 resize 方法来提高性能并避免内存不足错误。该工具将负责重用位图及其缓存。

https://github.com/square/picasso

https://github.com/bumptech/glide

有了毕加索,就这么简单:

Picasso.with(context).load(R.drawable.drawableName).resize(50, 50).centerCrop().into(imageView);

【讨论】:

  • 谢谢。但是我可以使用毕加索在自定义的网格视图中加载图像吗?
  • 好吧,我想网格项目有一些图像视图。而在你将图像分配给图像视图的那一刻,只需使用毕加索。
  • 我使用了 listFlag1.add(Picasso.with(getApplicationContext()).load(R.drawable.w1).get());当 listflag 是位图数组并作为 gridviewadapter 类的图像源传递时。但是当我使用它时,gridview1 中没有显示任何内容。为什么会发生?我做错了什么?
  • 这样就行不通了,因为毕加索不会立即返回位图——它是一个异步工具。您应该按照我上面发布的方式使用它。为此,您需要实现自定义适配器和自定义视图,以更好地控制绘图。检查:stackoverflow.com/a/19699602/2951744
  • 你能说说异步工具吗?
猜你喜欢
  • 2015-11-29
  • 2010-10-23
  • 1970-01-01
  • 1970-01-01
  • 2018-06-26
  • 2020-07-26
  • 2016-02-07
  • 1970-01-01
  • 2018-07-10
相关资源
最近更新 更多