首页 > 代码库 > 保存文件到手机内存
保存文件到手机内存
一 UI Code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tv_weather"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
</RelativeLayout>
二 Logical Code
<?xml version=‘1.0‘ encoding=‘utf-8‘ standalone=‘yes‘ ?>
<weather>
<day id="1">
<wendu>18</wendu>
<wind>5</wind>
<type>晴</type>
</day>
<day id="2">
<wendu>16</wendu>
<wind>3</wind>
<type>雨</type>
</day>
<day id="3">
<wendu>19</wendu>
<wind>4</wind>
<type>阴</type>
</day>
</weather>
package com.itheima.weather.service;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.util.Xml;
import com.itheima.weather.domain.Weather;
public class WeatherService {
/**
* 解析获取天气信息
* 天气信息xml文件对应的流
* @throws Exception
*/
public static List<Weather> getWeather(InputStream is) throws Exception {
// 解析 天气的xml文件.
// 1.获取到一个xml文件的解析器.
XmlPullParser parser = Xml.newPullParser();
// 2.初始化解析器.
parser.setInput(is, "utf-8");
// 3.解析xml文件.
// 得到当前解析条目的节点类型.
int eventType = parser.getEventType(); // 第一次被调用的时候 定位在xml开头
List<Weather> weatherInfos = null;
Weather weatherInfo = null;
while (eventType != XmlPullParser.END_DOCUMENT) {// 需要不停的让 解析器解析下一个节点
switch (eventType) {
case XmlPullParser.START_TAG:
if ("weather".equals(parser.getName())) {
// 发现开始节点 为weather 创建集合
weatherInfos = new ArrayList<Weather>();
} else if ("day".equals(parser.getName())) {
// 发现一个新的日期 对应的天气
weatherInfo = new Weather();
String id = parser.getAttributeValue(0);
weatherInfo.setId(Integer.parseInt(id));
} else if ("wendu".equals(parser.getName())) {
String wendu = parser.nextText();
weatherInfo.setWendu(Integer.parseInt(wendu));
} else if ("wind".equals(parser.getName())) {
String wind = parser.nextText();
weatherInfo.setWind(Integer.parseInt(wind));
} else if ("type".equals(parser.getName())) {
String type = parser.nextText();
weatherInfo.setType(type);
}
break;
case XmlPullParser.END_TAG:
if ("day".equals(parser.getName())) {
weatherInfos.add(weatherInfo);
weatherInfo=null;
}
break;
}
eventType = parser.next();// 控制解析器 解析下一个节点
}
is.close();
return weatherInfos;
}
}
package com.itheima.weather.domain;
public class Weather {
private int wendu;
private int wind;
private String type;
private int id;
public int getWendu() {
return wendu;
}
public void setWendu(int wendu) {
this.wendu = wendu;
}
public int getWind() {
return wind;
}
public void setWind(int wind) {
this.wind = wind;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "天气信息 [温度=" + wendu + ", 风力=" + wind + "级 , 天气状况=" + type
+ ", 未来第=" + id + "天]";
}
}
package com.itheima.weather;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import com.itheima.weather.domain.Weather;
import com.itheima.weather.service.WeatherService;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.tv_weather);
try {
StringBuilder sb = new StringBuilder();
List<Weather> weatherinfos = WeatherService.getWeather(getClassLoader().getResourceAsStream("weather.xml"));
for(Weather weather : weatherinfos){
sb.append(weather.toString());
sb.append("\n");
}
tv.setText(sb.toString());
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "解析天气信息失败", 0).show();
}
}
}
保存文件到手机内存