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;}

更多相关文章

  1. Android 我的 Android Studio 配置文件
  2. android文件操作OpenFileInput OpenFileOutput
  3. Android 编写使用root权限的android应用程序
  4. Android拷贝图片到指定文件路径
  5. android 安卓创建文件夹
  6. Android 解析excel文件
  7. AndroidManifest.xml 系统找不到指定的文件
  8. Smail反汇编文件语句说明
  9. Android 复制单个文件到指定目录,Android copy file

随机推荐

  1. android 多媒体扫描过程(Android(安卓)Med
  2. Excessive JNI global references - Andr
  3. Android(安卓)Studio第三十九期 - popWin
  4. Android——ViewPager+Fragment+ListView
  5. Android7.0中文文档(API)-- LinearLayout.L
  6. android 微信 listview 气泡
  7. imagebutton 带文字
  8. 【Android】安卓运行问题
  9. Android’s HTTP Clients(Android的HTTP客
  10. android典型代码系列(四)------android全