【发布时间】:2013-11-15 06:36:37
【问题描述】:
我是 android 应用程序开发的新手。谁能建议我如何在 android 中使用 JSON 解析在 gridview 中显示图像?
【问题讨论】:
我是 android 应用程序开发的新手。谁能建议我如何在 android 中使用 JSON 解析在 gridview 中显示图像?
【问题讨论】:
您究竟想要做什么,因为 json 解析和在 gridview 中显示图像是两件不同的事情。要么您想从服务器获取图像并使用 json 解析来解析 URL。或者让您的问题清晰易懂。
【讨论】:
在你的项目中添加这个类: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;
}
}
在主要活动中:- 添加网格视图并设置适配器 用你的 URL 调用 asyntask
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);
【讨论】: