﻿{"id":1806,"date":"2012-04-20T20:59:36","date_gmt":"2012-04-20T12:59:36","guid":{"rendered":"http:\/\/blog.jixin.ntxz.net\/?p=1806"},"modified":"2012-04-20T20:59:36","modified_gmt":"2012-04-20T12:59:36","slug":"android%e5%bc%80%e5%8f%91%e4%b9%8bsd%e5%8d%a1%e4%b8%8a%e6%96%87%e4%bb%b6%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"http:\/\/www.ntxz.net\/?p=1806","title":{"rendered":"Android\u5f00\u53d1\u4e4bSD\u5361\u4e0a\u6587\u4ef6\u64cd\u4f5c"},"content":{"rendered":"<p>1.       \u5f97\u5230\u5b58\u50a8\u8bbe\u5907\u7684\u76ee\u5f55:\/SDCARD\uff08\u4e00\u822c\u60c5\u51b5\u4e0b\uff09:<\/p>\n<p>SDPATH=Environment.getExternalStorageDirectory()+&#8221;\/&#8221;;<\/p>\n<p>2.       \u5224\u65adSD\u5361\u4e0a\u7684\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\uff1a\u901a\u8fc7File\u5bf9\u8c61\u7684exists()\u65b9\u6cd5\u3002<\/p>\n<p>  \/**<br \/>\n      * \u5224\u65ad\u6587\u4ef6\u662f\u5426\u5df2\u7ecf\u5b58\u5728;<br \/>\n      *\/<br \/>\n     public boolean checkFileExists(String filepath) {<br \/>\n           File file=new File(SDPATH+filepath);<br \/>\n           return file.exists();<br \/>\n    }<\/p>\n<p>3.       \u5728SD\u5361\u4e0a\u521b\u5efa\u76ee\u5f55\uff1a\u901a\u8fc7File\u5bf9\u8c61\u7684mkdir()\u65b9\u6cd5\u5b9e\u73b0\u3002<\/p>\n<p>  \/*<br \/>\n     * \u5728SD\u5361\u4e0a\u521b\u5efa\u76ee\u5f55\uff1b<br \/>\n      *\/<br \/>\n     public File createDIR(String dirpath) {<br \/>\n    File dir=new File(SDPATH+dirpath);<br \/>\n   dir.mkdir();<br \/>\n    return dir;<br \/>\n}<\/p>\n<p>4.       \u5728SD\u5361\u4e0a\u521b\u5efa\u6587\u4ef6\uff1a\u901a\u8fc7File\u5bf9\u8c61\u7684createNewFile()\u65b9\u6cd5\u5b9e\u73b0\u3002<\/p>\n<p>  \/*<br \/>\n      * \u5728SD\u5361\u4e0a\u521b\u5efa\u6587\u4ef6\uff1b<br \/>\n      *\/<br \/>\n     public File createFile(String filepath) throws IOException{<br \/>\n      File file=new File(SDPATH+filepath);<br \/>\n          file.createNewFile();<br \/>\n          return file;<br \/>\n     }<\/p>\n<p>5.       \u5c06InputStream\u5b57\u8282\u6d41\u5199\u5165\u5230SD\u5361\u6587\u4ef6\u4e2d\u3002<\/p>\n<p>     \/**<br \/>\n      * \u5c06\u4e00\u4e2aInputStream\u4e2d\u7684\u6570\u636e\u5199\u5165\u81f3SD\u5361\u4e2d<br \/>\n      *\/<br \/>\n     public File writeStreamToSDCard(String dirpath,String filename,InputStream input) {<\/p>\n<p>             File file = null;<br \/>\n             OutputStream output=null;<br \/>\n              try {<br \/>\n                  \/\/\u521b\u5efa\u76ee\u5f55\uff1b<br \/>\n                  createDIR(dirpath);<br \/>\n                  \/\/\u5728\u521b\u5efa \u7684\u76ee\u5f55\u4e0a\u521b\u5efa\u6587\u4ef6\uff1b<br \/>\n                  file = createFile(dirpath+filename);<br \/>\n                  output=new FileOutputStream(file);<br \/>\n                  byte[]bt=new byte[4*1024];<br \/>\n                  while (input.read(bt)!=-1) {<br \/>\n                     output.write(bt);<br \/>\n                  }<\/p>\n<p>                                  \/\/\u5237\u65b0\u7f13\u5b58\uff0c<\/p>\n<p>                  output.flush();<br \/>\n              } catch (IOException e) {<br \/>\n                  e.printStackTrace();<br \/>\n              }<br \/>\n              finally{<br \/>\n                  try{<br \/>\n                  output.close();<br \/>\n                  }catch (Exception e) {<br \/>\n                     e.printStackTrace();<br \/>\n                  }<br \/>\n              }<br \/>\n             return file;<br \/>\n    }<\/p>\n<p>6.       \u8bbf\u95ee\u7684\u6743\u9650\uff1a<\/p>\n<p>\u9700\u5728AndroidManifest\u4e2d\u52a0\u4e0a\uff1a<\/p>\n<p>   <uses-permission android:name=\"android.permission.INTERNET\"><\/uses-permission><\/p>\n<p><uses-permission android:name=\"android.permission.WRITE_EXTERNAL_STORAGE\"><\/uses-permission><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u5f97\u5230\u5b58\u50a8\u8bbe\u5907\u7684\u76ee\u5f55:\/SDCARD\uff08\u4e00\u822c\u60c5\u51b5\u4e0b\uff09: SDPATH=Environment.getExte [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[346],"tags":[518],"class_list":["post-1806","post","type-post","status-publish","format-standard","hentry","category-android","tag-sd"],"views":374,"_links":{"self":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts\/1806","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1806"}],"version-history":[{"count":1,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts\/1806\/revisions"}],"predecessor-version":[{"id":1807,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts\/1806\/revisions\/1807"}],"wp:attachment":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1806"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}