Reputation: 984
In my android app. I have written an asynctask for client server requests and in the doInBackground part I have obtained the server response to the android app as json. The code is below:
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);
}
This is in the doinBackground section of the asynctask. Now in the onpostexecute part, I have written the following code.
@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 is the name of my class in my project.. I will show the code of 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;
}
}
When I try to display the images in the gridview using the imageview. It is not showing any images on my activity. Although there are Bitmaps in the bitmap arraylist..Can someone point out the error in this code..Thanks in advance...
Upvotes: 0
Views: 425
Reputation: 11214
getCount() returns 0. It should return mImageArray.size().
Further remove the for loop from getView(). The setImageBitmap() has to be done always. Do it just before return i;
Upvotes: 1
Reputation: 1446
/******* 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 part does not look good. You are getting imagename from json as a string & trying to convert that to a Bitmap. Instead, try this approach: -Get image name or URL from JSON ( in this rest call ) -Then, get the image from the url in a background thread / async task / service.
You can find the best way to do @ http://developer.android.com/training/displaying-bitmaps/index.html
Or, if you want to go one step ahead, refer picasso https://github.com/square/picasso It is the one I referred for one of my previous projects.
Upvotes: 0