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<UrlBean> getJsonData(String url) {
List<UrlBean> 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<String, Void, List<UrlBean>> {
@Override
protected List<UrlBean> doInBackground(String... params) {
return getJsonData(params[0]);//URL获取JSON数据
}
@Override
protected void onPostExecute(List<UrlBean> urlBeans) {
super.onPostExecute(urlBeans);
for (int i = 0 ; i<urlBeans.size(); i++) {
new MyImageAsyncTask().execute(urlBeans.get(i).imgUrl);
}
}
}
//通过没一个图片的地址请求一次网络得到图片
class MyImageAsyncTask extends AsyncTask<String, Void, View>{
@Override
protected View doInBackground(String... params) {
Bitmap bitmap = getBitmapFromURL(params[0]);
ImageView imageView = getImageView(bitmap);
return imageView;
}
@Override
protected void onPostExecute(View view) {
super.onPostExecute(view);
views.add(view);
if (views.size()>3){
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<View> views){
LinearLayout ll = (LinearLayout) findViewById(R.id.linear);
for (int i = 0; i<views.size(); i++){
ll.addView(views.get(i));
}
}