I am new in android apps development. Can anybody suggest me how to display images in a gridview using JSON parsing in android?
2 Answers
add this class in your project: JsonParser
public class ImageDownloader extends AsyncTask<String, Void, Bitmap> {
@Override
protected Bitmap doInBackground(String... params) {
// TODO Auto-generated method stub
Bitmap bitmap = null;
try {
URL aURL = new URL(params[0]);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
// Buffered is always good for a performance plus.
BufferedInputStream bis = new BufferedInputStream(is);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize =5;
// Decode url-data to a bitmap.
bitmap = BitmapFactory.decodeStream(bis, null, options);
// Close BufferedInputStream
bis.close();
// Close InputStream
is.close();
return bitmap;
} catch (IOException e1) {
e1.printStackTrace();
return null;
}
}
in main activity:- add grid view and set adapter call the asyntask with your URL
ImageDownloader d=new ImageDownloder();
Bimap b=d.execute("url").get();
grid = (GridView) findViewById(R.id.grid);// grid view ti view all photos
AdapterForPics adap = new AdapterForPics(Arraylist, Context);
grid.setAdapter(adap);