2013-12-23 71 views
0

我試圖實現存儲在sqlite數據庫中的圖像的縮略圖視圖。但它甚至沒有顯示'文件夾爲空'的通知。請任何人幫我糾正錯誤。在android中的縮略圖問題

代碼片段

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]; 
      }  } 
} 

回答

0

一個問題是,imageBitmaps是一個長度爲100的數組,你getCount()返回imageBitmaps.length。這可能掩蓋了這樣一個事實,即您的光標邏輯可能會被破壞,並且您實際上從未添加任何圖像。

將imageBitmap更改爲ArrayList並致電imageBitmaps.add(BitmapFactory.decodeStream(inputStream),以便getCount()將正確返回。如果在此之後長度爲0,那麼你知道問題在於你的遊標邏輯。