通过JSON文件获取图片URL,下载并显示在布局中

您所在的位置:网站首页 安卓json文件放在哪 通过JSON文件获取图片URL,下载并显示在布局中

通过JSON文件获取图片URL,下载并显示在布局中

2023-07-27 00:27| 来源: 网络整理| 查看: 265

1.AsyncTask来实现网络的异步加载

2.获取JSON数据的图片地址,在用图片地址去下载图片

3.把下载下来的图片保存在文件里

4.加载到布局上

遗留问题:缓存怎么处理?

JSON代码:

{"status":1,"img": [ { "id":"1", "imageurl":"http://192.168.31.152/image/b1.jpg" }, { "id":"2", "imageurl":"http://192.168.31.152/image/b2.jpg" }, { "id":"3", "imageurl":"http://192.168.31.152/image/b3.jpg" }, { "id":"4", "imageurl":"http://h.hiphotos.baidu.com/news/q%3D100/sign=d38d087d5bdf8db1ba2e78643921dddb/9345d688d43f87942b2f41cbd71b0ef41ad53a06.jpg" }, ] }

新建一个Bean类用来存储对应的属性

public class UrlBean { public String imgUrl; }

获取JSON数据的方法,并返回Bean对象

/** * 获取Json数据 * * @return */ private List getJsonData(String url) { List lists = new ArrayList(); try { String jsonString = readStream(new URL(imgURL).openStream()); JSONObject jsonObject; UrlBean urlBean; try { jsonObject = new JSONObject(jsonString); JSONArray jsonArray = jsonObject.getJSONArray("img"); for (int i = 0; i < jsonArray.length(); i++) { jsonObject = jsonArray.getJSONObject(i); urlBean = new UrlBean(); urlBean.imgUrl = jsonObject.getString("imageurl"); lists.add(urlBean); } } catch (JSONException e) { e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } return lists; } /** * 输入流-字符输入流 返回字符串 * @param is * @return */ private String readStream(InputStream is) { InputStreamReader isr; String result = ""; try { String line = ""; isr = new InputStreamReader(is, "utf-8"); BufferedReader bufferedReader = new BufferedReader(isr); while ((line = bufferedReader.readLine()) != null) { result += line; } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return result; } 用两个异步加载的方法分别读取JSON和循环读取每一张图片(应该又更好的方法把)

/** * 异步加载 * @return */ class MyAsyncTask extends AsyncTask { @Override protected List doInBackground(String... params) { return getJsonData(params[0]);//URL获取JSON数据 } @Override protected void onPostExecute(List urlBeans) { super.onPostExecute(urlBeans); for (int i = 0 ; i3){ setlayoutImage(views); } } }; 通过JSON里的每一个图片的地址,来获取Bitmap对象。在得到输入流时,做了存入SD卡操作

private File shen; private File sdcard; private int x; //笨方法,动态循环更改文件名 public Bitmap getBitmapFromURL(String urlString){ Bitmap bitmap; InputStream is = null; FileOutputStream fos; try { URL url = new URL(urlString); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); is = new BufferedInputStream(connection.getInputStream()); bitmap = BitmapFactory.decodeStream(is); //直接把输入流存到SD卡里 if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { sdcard = Environment.getExternalStorageDirectory(); shen = new File(sdcard, "ss/a" + x + ".jpg"); x = x+1; Log.i("ccc", "ss/"+urlString); fos = new FileOutputStream(shen); byte[] b = new byte[2 * 1024]; int len; if (is != null) { while ((len = is.read(b)) != -1) { fos.write(b, 0, len); } } //只做了下载图片,没做缓存,暂时还不知道怎么做 //bitmap = BitmapFactory.decodeFile(shen.getAbsolutePath()); } connection.disconnect(); return bitmap; } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } Bitmap 返回 ImageView

public ImageView getImageView(Bitmap bitmap) { ImageView imageView = new ImageView(this); imageView.setImageBitmap(bitmap); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); return imageView; } 把图片加载到布局中(这里或许可以不用List对象的 得到一个图加载一个图)

public void setlayoutImage(List views){ LinearLayout ll = (LinearLayout) findViewById(R.id.linear); for (int i = 0; i


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3