图像位图的位图数组列表中的图像网格视图中未显示的图像



在我的android应用程序中。我为客户端-服务器请求编写了一个异步任务,在doInBackground部分中,我获得了服务器对android应用程序的json响应。代码如下:

String status=sb.toString();
JSONObject jsonResponse1;
try {
/****** Creates a new JSONObject with name/value mappings from the JSON string. ********/
jsonResponse1 = new JSONObject(status);
/***** Returns the value mapped by name if it exists and is a JSONArray. Returns null otherwise.*******/
JSONArray jsonMainNode=jsonResponse1.optJSONArray("Android");
/*********** Process each JSON Node ************/
int lengthJsonArr = jsonMainNode.length();
Log.d("Json Array Length",String.valueOf(lengthJsonArr));
    for(int j1=0;j1<lengthJsonArr;j1++)
    {
        /****** Get Object for each JSON node.***********/
        JSONObject jsonChildNode = jsonMainNode.getJSONObject(j1);
        /******* Fetch node values **********/
        String index=jsonChildNode.optString("index").toString();
        String imagename=jsonChildNode.optString("imagename").toString();
        byte[] decodedString = Base64.decode(imagename, Base64.DEFAULT);
        decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
        imagearraylist.add(decodedByte);
        //This arraylist is an Bitmap arraylist and contains the Bitmaps of two images received from the server..
    }           
  }
catch(Exception ex)
{
       System.out.print(ex);
}

这是在asynctask的doinBackground部分中。现在,在onpostexecute部分,我已经编写了以下代码。

@Override
protected void onPostExecute(Void result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);
    Toast.makeText(Gallery.this, "Loading complete", Toast.LENGTH_LONG).show();
    pd.dismiss();
    gridView.setAdapter(new ImageAdapter2(getApplicationContext(),imagearraylist));
}

ImageAdapter2是我项目中的类的名称。。我将展示ImageAdapter2.class 的代码

package com.example.mygallery;
//skipping the import section
public class ImageAdapter2 extends BaseAdapter{
private Context mContext;       
ArrayList<Bitmap>mImageArray;   
public ImageAdapter2(Context c,ArrayList<Bitmap> imgArray) {
    // TODO Auto-generated constructor stub
    mContext=c;
    mImageArray=imgArray;
    Log.d("Entered into imageadapter2","Success");
    System.out.println("Arr len: "+imgArray.size());
     //Here I get output as 2 in the logcat.Upto this portion the code is executing fine..
 //After this the getView function is not executing. What is the error in this?? Can someone point it out...
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return 0;
}
@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}
@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ImageView i = null ;
    int size=mImageArray.size();
    System.out.println("sze: "+size);
    Log.d("Size of Image bitmap array",String.valueOf(size));
    if (convertView == null ) 
    {               
        i = new ImageView(mContext);
        for(int i1=0;i1<size;i1++)
        {
            i.setImageBitmap(mImageArray.get(i1));     
        }
    }
     else 
         i = (ImageView) convertView;           
     return i;
}

}

当我尝试使用imageview在gridview中显示图像时。它没有显示任何关于我活动的图像。尽管位图数组列表中有位图。。有人能指出这段代码中的错误吗。。提前感谢。。。

getCount((返回0。它应该返回mImageArray.size((.

进一步从getView((中删除for循环。setImageBitmap((必须始终执行。在我回来之前做;

    /******* Fetch node values **********/
    String index=jsonChildNode.optString("index").toString();
    String imagename=jsonChildNode.optString("imagename").toString();
    byte[] decodedString = Base64.decode(imagename, Base64.DEFAULT);
    decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
    imagearraylist.add(decodedByte);

这部分看起来不太好。您将从json中获取字符串形式的imagename;试图将其转换为位图。相反,请尝试以下方法:-从JSON中获取图像名称或URL(在此rest调用中(-然后,从后台线程/async任务/服务中的url获取图像。

你可以找到最好的方法@http://developer.android.com/training/displaying-bitmaps/index.html

或者,如果你想领先一步,请推荐毕加索https://github.com/square/picasso这是我在以前的一个项目中提到的那个。

最新更新