【问题标题】:Unable to view images properly in a GridView无法在 GridView 中正确查看图像
【发布时间】:2013-12-19 05:12:24
【问题描述】:

MainActivity:

public class AndroidCustomGalleryActivity extends Activity {
    private int count;
    private Bitmap[] thumbnails;
    private boolean[] thumbnailsselection;
    private String[] arrPath;
    private ImageAdapter imageAdapter;
    ArrayList<String> f = new ArrayList<String>();// list of file paths
    File[] listFile;
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_android_custom_gallery);
        getFromSdcard();
        GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
        imageAdapter = new ImageAdapter();
        imagegrid.setAdapter(imageAdapter);


    }
    public void getFromSdcard()
    {
        File file= new File(android.os.Environment.getExternalStorageDirectory(),"SnapBoard");

            if (file.isDirectory())
            {
                listFile = file.listFiles();


                for (int i = 0; i < listFile.length; i++)
                {

                    f.add(listFile[i].getAbsolutePath());

                }
            }
    }

    public class ImageAdapter extends BaseAdapter {
        private LayoutInflater mInflater;

        public ImageAdapter() {
            mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        public int getCount() {
            return f.size();
        }

        public Object getItem(int position) {
            return position;
        }

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

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(
                        R.layout.galleryitem, null);
                holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);

                convertView.setTag(holder);
            }
            else {
                holder = (ViewHolder) convertView.getTag();
            }


            Bitmap myBitmap = BitmapFactory.decodeFile(f.get(position));
            holder.imageview.setImageBitmap(myBitmap);
            return convertView;
        }
    }
    class ViewHolder {
        ImageView imageview;


    }
        }

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >


<GridView
    android:id="@+id/PhoneImageGrid"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:gravity="center"
    android:horizontalSpacing="10dp"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:verticalSpacing="10dp" />


</RelativeLayout>

GalleryItem.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/thumbImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

    <CheckBox
        android:id="@+id/itemCheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>

快照:

我能够从我的 SD 卡的单独文件夹中检索图像并将其显示在网格视图中,但视图似乎相当折叠。和屏幕大小有关系吗?我使用的是 Galaxy ACE DUOS(3.5 英寸)。将尝试尽快发布屏幕截图。帮助将不胜感激。提前致谢。

编辑:

Logcat:

12-19 11:08:25.335: D/dalvikvm(22171): GC_EXTERNAL_ALLOC freed 66K, 47% free 2957K/5511K, external 58486K/58486K, paused 39ms
12-19 11:08:25.351: E/dalvikvm-heap(22171): 9830400-byte external allocation too large for this process.
12-19 11:08:25.390: E/GraphicsJNI(22171): VM won't let us allocate 9830400 bytes
12-19 11:08:25.390: D/dalvikvm(22171): GC_FOR_MALLOC freed <1K, 47% free 2957K/5511K, external 58486K/58486K, paused 30ms
12-19 11:08:25.390: D/skia(22171): --- decoder->decode returned false

【问题讨论】:

    标签: android gridview imageview


    【解决方案1】:

    与其在充气机中使用单独的 imageview,不如从充气机中删除 imageview,并将 thnumbnail 设置为充气机 relativewlayout 的背景。

    【讨论】:

      【解决方案2】:

      我认为你的问题是当你获得网格视图的视图时有错误。

      错误

      public Object getItem(int position) {
              return position;
      }
      

      更正

      public Object getItem(int position) {
              return f.get(position);
      }
      

      错误是因为每当您显示图像时,您都会在虚拟内存中分配图像大小。这可以通过调整位图大小并减小大小然后显示图像来覆盖。

      public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
      int width = bm.getWidth();
      int height = bm.getHeight();
      float scaleWidth = ((float) newWidth) / width;
      float scaleHeight = ((float) newHeight) / height;
      // CREATE A MATRIX FOR THE MANIPULATION
      Matrix matrix = new Matrix();
      // RESIZE THE BIT MAP
      matrix.postScale(scaleWidth, scaleHeight);
      
      // "RECREATE" THE NEW BITMAP
      Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
      return resizedBitmap;
      }
      

      在你的代码中

      Bitmap myresizedbitmap= getResizedBitmap(myBitmap,300, 300);
      holder.imageview.setImageBitmap(myresizedbitmap);
      

      【讨论】:

      • 感谢您的回复。我已经尝试过了,但仍然得到与上面发布的相同的图像类型。我还发布了我的 logcat。如果可能的话,请看一下。再次感谢您的努力。欣赏它。
      • 所以我应该在 holder.imageview.setImageBitmap(myBitmap); 之后的 getView 调用 getResizedBitmap ?
      猜你喜欢
      • 2016-01-01
      • 2012-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多