常用的android权限配置和常用工具代码

1、访问权限的配置

<!-- 访问internet权限 -->
<uses-permission android:name="android.permission.INTERNET"/>
<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

2、MD5加密

public class MD5 {
	public static String getMD5(String content) {
		try {
			MessageDigest digest = MessageDigest.getInstance("MD5");
			digest.update(content.getBytes());
			return getHashString(digest);
			
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return null;
	}	
  private static String getHashString(MessageDigest digest) {
      StringBuilder builder = new StringBuilder();
      for (byte b : digest.digest()) {
          builder.append(Integer.toHexString((b >> 4) & 0xf));
          builder.append(Integer.toHexString(b & 0xf));
      }
      return builder.toString();
  }
}

3、XmlPullParser解析xml文件返回List对象

/*
* 根据提供的URL,得到HttpURLConnection的连接,设置超时时间和请求方式
* 使用XMLPullParser解析请求的输入流,循环解析xml,直到文档的结尾
* <?xml version="1.0" encoding="UTF-8"?>
  <topics>
    <topic id="1"> 
    <title>西安大雁塔01</title> 
    <image src="http://192.168.173.1:8081/mysap/images/0101.jpg"/> 
  </topic>
 </topics>
*/
public List<Topic> getContacts() throws Exception{
    String dataURL = "http://192.168.173.1:8081/mysap/datafile/topic01.xml";
    HttpURLConnection conn = (HttpURLConnection)new URL(dataURL).openConnection();
    conn.setConnectTimeout(5000);
    conn.setRequestMethod("GET");
    if(conn.getResponseCode() == 200){
      InputStream inputStream = conn.getInputStream();
      String inputEncoding = "UTF-8";
      XmlPullParser xmlParser = Xml.newPullParser();
      xmlParser.setInput(inputStream, inputEncoding);
      List<Topic> topicList = new ArrayList<Topic>();
      int eventType = xmlParser.getEventType();
      String tagName = null;
      Topic topic = null;
      while(eventType != XmlPullParser.END_DOCUMENT){
	  tagName = xmlParser.getName();
	  switch(eventType){
	   case XmlPullParser.START_TAG:
	      if("topic".equals(tagName)){
	         topic = new Topic();
	         topic.id = new Integer(xmlParser.getAttributeValue(0));
	      }else if("title".equals(tagName)){
	         topic.title = xmlParser.nextText();
	      }else if("image".equals(tagName)){
	         topic.imageSrc = xmlParser.getAttributeValue(0);
	      }
	      break;
	   case XmlPullParser.END_TAG:
	      if("topic".equals(tagName)){
		topicList.add(topic);
		topic = null;
              }
	      break;
	   }
	   eventType = xmlParser.next();
	}
	return topicList;
     }
     return null;
}

4、从网络上获取文件,存储在指定的缓存目录下

/**
 * 先把网络文件的fileUri进行MD5加密处理,然后生产对应的文件(后缀名不变)
 * 先从缓存目录下查询是否存在该文件,如果不存在则再次获取,直接从缓存中获取
 * 否则把网络文件缓存到本地
 * @param fileUri
 * @param cacheDir
 * @return
 * @throws Exception
 */
public Uri getImage(String fileUri, File cacheDir) throws Exception{
	String tempPath = MD5.getMD5(fileUri) + 
			fileUri.substring(fileUri.lastIndexOf("."));
	File localFile = new File(cacheDir, tempPath);
	if(localFile.exists()){
		return Uri.fromFile(localFile);
	}else{
		HttpURLConnection conn = (HttpURLConnection)new URL(fileUri).openConnection();
		conn.setConnectTimeout(5000);
		conn.setRequestMethod("GET");
		if(conn.getResponseCode() == 200){
			FileOutputStream outStream = new FileOutputStream(localFile);
			InputStream inputStream = conn.getInputStream();
			byte[] buffer = new byte[1024];
			int len = 0;
			while( (len = inputStream.read(buffer)) != -1){
				outStream.write(buffer, 0, len);
			}
			inputStream.close();
			outStream.close();
			return Uri.fromFile(localFile);
		}
	}
	return null;		
}

猜你喜欢

转载自zhengjj-2009.iteye.com/blog/1841945