我有一些图像存储在内部存储器中。我设法检索图像文件位置并对其进行解码。但我无法让它显示在网格视图中。而且我不确定代码有什么问题,因为目前没有错误。任何意见将不胜感激。
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.web_tab);
helper = new DBHelper(this);
Object[] values = helper.get_contentByEmailID(EMAIL);
this.arrPath = new String[this.count];
this.thumbnailsselection = new boolean[this.count];
Log.i(TAG, "values:" +values);
Log.i(TAG, "filepath:" +values[0]);
Log.i(TAG, "filepath:" +values[1]);
Log.i(TAG, "values:" +values.length);
if(values.length>0){
for (int i=0;i<values.length;i++){
Log.i(TAG, "values[]" +values[i]);
String bImage = (String) values[i];
bitmap = new Bitmap [this.count];
bitmap = decodeFile(bImage);
Log.i(TAG, "bImage"+i+":" +bImage);
Log.i(TAG, "bitmap"+i+":" +bitmap);
}
}
else{
Log.i(TAG, "Unable to locate images");
}
imagegrid = (GridView) findViewById(R.id.WebImageGrid);
imageAdapter = new ImageAdapter();
imagegrid.setAdapter(imageAdapter);
}
下面是 ImageAdapter 代码。
public class ImageAdapter extends BaseAdapter
{
private Context mContext;
Bitmap[] mImageArray;
private LayoutInflater mInflater;
public ImageAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return count;
}
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);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.checkbox.setId(position);
holder.imageview.setId(position);
holder.imageview.setImageBitmap(bitmap[position]);
holder.checkbox.setChecked(thumbnailsselection[position]);
holder.id = position;
return convertView;
}
}
class ViewHolder {
ImageView imageview;
CheckBox checkbox;
int id;
}
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);
return bitmap;
}
更新
Object[] values = helper.get_wbm_synccontentByEmailID(SettingConstant.EMAIL);
count=values.length;
this.arrPath = new String[count];
this.thumbnailsselection = new boolean[count];
Log.i(TAG, "values:" +values.length);
String bImage;
if(count>0){
bitmap = new Bitmap [count];
for (int i=0;i<count;i++){
Log.i(TAG, "values[]" +values[i]);
bImage = (String) values[i];
Bitmap newBitmap = decodeFile(bImage);
this.arrPath[i] = bImage;
this.bitmap[i] = newBitmap;
}
public Bitmap decodeFile(String filePath)
{
System.out.println("filepath in decode file .. "+filePath);
Bitmap bitmapnew = BitmapFactory.decodeFile(filePath);
return bitmapnew;
}