android中的缩略图视图问题

时间:2013-12-23 01:32:46

标签: android thumbnails android-sqlite

我试图实现存储在sqlite数据库中的图像的缩略图视图。但它甚至没有显示“文件夹空”通知。请任何人帮我纠正错误。

代码段

   public class PicAdapter extends BaseAdapter {

            //use the default gallery background image
            int defaultItemBackground;
            //gallery context
            private Context galleryContext;
            //array to store bitmaps to display
            private Bitmap[] imageBitmaps;
            private ByteArrayInputStream inputStream;
            //placeholder bitmap for empty spaces in gallery
            Bitmap placeholder;
            public PicAdapter(Context c) {
                //instantiate context
                galleryContext = c;
                //create bitmap array
                imageBitmaps  = new Bitmap[100];
                    db = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READONLY);
                    if(db != null){
                    cursor = db.rawQuery("SELECT * FROM Images WHERE myId =?",new String[]{id},null);
                    if(cursor!=null)
                    {
                         // looping through all rows and adding to list
                        if (cursor.moveToFirst()) {
                            do {
                        displayToast("inside loop");
                            img[i] = cursor.getBlob(cursor.getColumnIndex("image"));
                            //title[i] = cursor.getString(cursor.getColumnIndex("title"));
                            //description[i] = cursor.getString(cursor.getColumnIndex("description"));
                            //txt1[i].setText(title[i]);
                            //txt2[i].setText(description[i]);

                         inputStream = new ByteArrayInputStream(img[i]);
                        imageBitmaps[i] = BitmapFactory.decodeStream(inputStream);                        
                          i++;
                            } while (cursor.moveToNext());
                            }
                    }else
                        displayToast("Your image folder is empty");

                    }

                        if (cursor != null && !cursor.isClosed()) {
                            cursor.close();
                        }
                        db.close();
                          }

                        //return number of data items i.e. bitmap images
            public int getCount() {
                return imageBitmaps.length;
            }
            //return item at specified position
            public Object getItem(int position) {
                return position;
            }
            //return item ID at specified position
            public long getItemId(int position) {
                return position;
            }
            //get view specifies layout and display options for each thumbnail in the gallery
            @SuppressWarnings("deprecation")
            public View getView(int position, View convertView, ViewGroup parent) {
                //create the view
                ImageView imageView = new ImageView(galleryContext);
                //specify the bitmap at this position in the array
                imageView.setImageBitmap(imageBitmaps[position]);
                //set layout options
                imageView.setLayoutParams(new Gallery.LayoutParams(300, 200));
                //scale type within view area
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                //set default gallery item background
                imageView.setBackgroundResource(defaultItemBackground);
                //return the view
                return imageView;
            }
                        //return bitmap at specified position for larger display
            public Bitmap getPic(int posn)
            {
                //return bitmap at posn index
                return imageBitmaps[posn];
            }       }
}

1 个答案:

答案 0 :(得分:0)

一个问题是,imageBitmaps是一个长度为100的数组,您在imageBitmaps.length中返回getCount()。这可能隐藏了这样一个事实,即您的光标逻辑可能已被破坏而您实际上从未添加任何图像。

将imageBitmaps更改为ArrayList并致电imageBitmaps.add(BitmapFactory.decodeStream(inputStream),以便getCount()正确返回。如果此后长度为0,那么您就知道问题出在光标逻辑中。