Reputation: 579
I am new in android apps development. Can anybody suggest me how to display images in a gridview using JSON parsing in android?
Upvotes: 1
Views: 1141
Reputation: 679
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);
Upvotes: 2
Reputation: 3822
what exactly you want to do because json parsing and display images in gridview are two diffrent things. either you want to fatch image from server and use json parsing to parse URLs .Or make your question clear to understand by everyone.
Upvotes: 3