/**
* 将图片文件保存到SD卡的根目录下
*
* 虽然确定SD卡的路径是可以直接使用"/sdcard"的,但在实际开发中建议使用:android.os.Environment.getExternalStorageDirectory()
* 方法获得SD卡的路径,这样一旦系统改变了路径,应用程序会立刻获得最新的SD卡的路径,这样做会使程序更健壮。
*/
public void writeToSD() {
try {
//创建用于将图片保存到SD卡上的FileOutputStream对象
FileOutputStream fos = new FileOutputStream(android.os.Environment.getExternalStorageDirectory() + "/image.jpg");
//打开assets目录下的image.jpg文件,并返回InputStream对象
InputStream is = getResources().getAssets().open("image.jpg");
//定义一个byte数组,用来保存每次向SD卡中文件写入的数据,最多8k
byte[] buffer = new byte[8192];
int count = 0;
//循环写入数据
while((count = is.read(buffer)) != -1)
{
fos.write(buffer, 0, count);
}
fos.close();
is.close();
Toast.makeText(this, "已成功将图片保存在SD卡中", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 从SD卡中读取图片文件
* @throws IOException
*/
public void readFromSD() throws IOException{
//指定SD卡中的图像文件名
String fileName = android.os.Environment.getExternalStorageState() + "image.jpg";
//判断文件图片是否存在
if (!new File(fileName).exists()) {
Toast.makeText(this, "没有要找的图片文件,未装入", Toast.LENGTH_SHORT).show();
return;
}
image = (ImageView) findViewById(R.id.image);
FileInputStream fis = new FileInputStream(fileName);
//从文件的输入流装载Bimap对象
Bitmap bitmap = BitmapFactory.decodeStream(fis);
image.setImageBitmap(bitmap);
fis.close();
}
<!-- 获取写权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<?xml version="1.0" encoding="utf-8"?>
<products>
<product>
<id>1</id>
<name>电脑</name>
<price>3088</price>
</product>
<product>
<id>2</id>
<name>微波炉</name>
<price>2500</price>
</product>
<product>
<id>3</id>
<name>洗衣机</name>
<price>1088</price>
</product>
</products>
package com.example.data_io_xmltojava;
public class Product {
int id;
String name;
int price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
package com.example.data_io_xmltojava;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class XML2Product extends DefaultHandler {
List<Product> products;
Product product;
StringBuffer sb = new StringBuffer();
public List<Product> getProduct() {
return products;
}
/**
* 开始分析XML文件
*/
@Override
public void startDocument() throws SAXException {
// 开始分析XML文件,创建list对象用于保存分析完的product对象
products = new ArrayList<Product>();
super.startDocument();
}
/**
* 开始分析XML中的标签
*/
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (localName.equals("product")) {
// 如果开始分析的是<product>标签,创建一个product对象
product = new Product();
}
super.startElement(uri, localName, qName, attributes);
}
/**
* 分析完了XML中的标签
* 使用sb中的值为product对象中的属性赋值
*/
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (localName.equals("product")) {
// 处理完<product>标签后,将product对象添加到products中
products.add(product);
} else if (localName.equals("id")) {
// 设置id属性值
product.setId(Integer.parseInt(sb.toString().trim()));
// 将保存标签内容的缓存区清空
sb.setLength(0);
} else if (localName.equals("name")) {
product.setName(sb.toString().trim());
sb.setLength(0);
} else if (localName.equals("price")) {
product.setPrice(Integer.parseInt(sb.toString().trim()));
sb.setLength(0);
}
super.endElement(uri, localName, qName);
}
/**
* 分析完了XML文件
*/
@Override
public void endDocument() throws SAXException {
super.endDocument();
}
/**
* 处理SAX读取到的XML文件中的内容
*/
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// 将SAX扫描到的内容保存到sb变量中
sb.append(ch, start, length);
super.characters(ch, start, length);
}
}
package com.example.data_io_xmltojava;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.xml.sax.SAXException;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.util.Xml;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获得 /res/raw/products.xml文件中InputStream对象
InputStream is = getResources().openRawResource(R.raw.products);
XML2Product xml2product = new XML2Product();
try {
// 开始分析products.xml文件(解析)
android.util.Xml.parse(is, Xml.Encoding.UTF_8, xml2product);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 将转换后得到的java对象的内容输出
List<Product> products = xml2product.getProduct();
String msg = "total" + products.size() + "\n";
for (Product product : products) {
msg += "id:" + product.getId() + "产品名:" + product.getName() + "价格"
+ product.getPrice() + "\n";
}
new AlertDialog.Builder(this).setTitle("产品信息").setMessage(msg)
.setPositiveButton("关闭", null).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有