2011-05-17 251 views
0

我有一些圖像路徑存儲在數據庫中,我必須在網格佈局中顯示圖像。 這裏是我的代碼在GridView中顯示圖像

public class HeadshotAllPhoto extends Activity 
{ 
    GridView imagegrid; 
    String path,filemanagerstring; 
    String[] ImageNameArr; 
    String filePath = null; 
    String ImageName ; 
    Bitmap bitmap; 
    DataHelperHeadshot dbHeadshot; 

String sub_list ; 
String[] pathArr; 
Bitmap[] bmp; 
ArrayList<String> items = new ArrayList<String>(); 
@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.headshotallphoto); 
    dbHeadshot = new DataHelperHeadshot(this); 

    bg = (ImageView)findViewById(R.id.selectHeadshotView); 

    List<String> names = this.dbHeadshot.fetchAllHeadshot(); 
    StringBuilder sb = new StringBuilder(); 
    for (String name : names) 
    { 
     name.trim(); 
     sb.append(","); 
     sb.append(name); 
    } 

    bmp = new Bitmap[icount+1];   
    sub_list = sb.toString();  
    Log.i("sub_list .. ",""+sub_list); 
    pathArr = sub_list.split(",");  

    for(int p=0;p<pathArr.length;p++)   
    {  
     if(pathArr[p]!=null) 
     {    
      bitmap = decodeFile(pathArr[p]);  
      if(bitmap!=null) 
      { 
       items.add(pathArr[p]);     
       bmp[p] = bitmap; 
      } 
     } 
     if(pathArr[p] == null) 
      break; 
    } 

    imagegrid = (GridView) findViewById(R.id.PhoneImageGrid); 
    imagegrid.setAdapter(new ImageAdapter(getApplicationContext(),bmp)); 
    imagegrid.setOnItemClickListener(this);  
    } 
} 

public class ImageAdapter extends BaseAdapter 
{ 
    private Context mContext;  
    Bitmap[] mImageArray; 

    public ImageAdapter(Context c, Bitmap[] imgArray) 
    { 
      mContext = c; 
      mImageArray = imgArray;    
    } 
    public int getCount() 
    { 
      return mImageArray.length; 
    } 
    public Object getItem(int position) 
    { 
      return position; 
    } 
    public long getItemId(int position) 
    { 
      return position; 
    } 

    public View getView(int position,View convertView,ViewGroup parent) 
    { 
     System.gc(); 
     ImageView i = null ; 

     if (convertView == null) 
     {    
      i = new ImageView(mContext); 
      i.setLayoutParams(new GridView.LayoutParams(92,92)); 
      i.setScaleType(ImageView.ScaleType.CENTER_CROP);    
      i.setImageBitmap(mImageArray[position]);       
     } 
     else 
      i = (ImageView) convertView;   
     return i; 
    }    
} 

public Bitmap decodeFile(String filePath) 
{ 
    System.out.println("filepath in decode file .. "+filePath); 
    BitmapFactory.Options o = new BitmapFactory.Options(); 
    o.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(filePath, o); 

    // The new size we want to scale to 
    final int REQUIRED_SIZE = 100; 
    final int H = 50; 

    // Find the correct scale value. It should be the power of 2. 
    int width_tmp = o.outWidth, height_tmp = o.outHeight; 
    int scale = 1; 
    while (true) { 
     if (width_tmp < REQUIRED_SIZE && height_tmp < H) 
      break; 
     width_tmp /= 2; 
     height_tmp /= 2; 
     scale *= 2; 
    } 
    // Decode with inSampleSize 
    BitmapFactory.Options o2 = new BitmapFactory.Options(); 
    o2.inSampleSize = scale; 
    System.out.println("decode file ........... "+filePath); 
    bitmap = BitmapFactory.decodeFile(filePath, o2);  
    return bitmap; 
} 
} 

請幫助我 感謝

+0

這裏是一個類似的帖子,http://stackoverflow.com/questions/4712945/showing-data-in-gridview-from-database – Mudassir 2011-05-17 06:14:15

+0

我取從數據庫的字符串路徑,並存儲在陣列的位圖但我每次得到1個額外的空圖像。 – Monali 2011-05-17 06:20:25

+0

可以告訴我代碼嗎? – Mudassir 2011-05-17 06:31:03

回答

0

我無法理解,如果你已經有了名單,爲什麼你創建了一個唱附加「」然後再次分裂它,是什麼原因造成的問題是,在您的字符串末尾有一個「,」導致添加空數據,BTW嘗試下面的代碼可能會有所幫助,更改您的onCreate b elow一個

protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.headshotallphoto); 
    dbHeadshot = new DataHelperHeadshot(this); 

    bg = (ImageView)findViewById(R.id.selectHeadshotView); 

    List<String> names = this.dbHeadshot.fetchAllHeadshot(); 
    StringBuilder sb = new StringBuilder(); 
    for (String name : names) 
    { 
     name.trim(); 
     sb.append(","); 
     sb.append(name); 
    } 

    bmp = new Bitmap[10+1];   

    for(int p=0;p<names.size();p++)   
    {  
     if(names.get(p) !=null) 
     {    
      bitmap = decodeFile(names.get(p));  
      if(bitmap!=null) 
      { 
       items.add(names.get(p));     
       bmp[p] = bitmap; 
      } 
     } 
     if(pathArr[p] == null) 
      break; 
    } 

    imagegrid = (GridView) findViewById(R.id.PhoneImageGrid); 
    imagegrid.setAdapter(new ImageAdapter(getApplicationContext(),bmp)); 
    imagegrid.setOnItemClickListener(this);  
    } 
} 
+0

感謝它對我有用 – Monali 2011-05-17 07:11:20

+0

難道你應該告訴我如何把刻度線放在我們點擊的圖像上嗎? – Monali 2011-05-17 08:51:22

+0

將其作爲單獨的問題和相關信息發佈 – ingsaurabh 2011-05-17 09:12:26