Import Java.io.filenotfoundexception;import java.io.fileoutputstream;import java.io.ioexception;import Android. R.integer;import Android.content.context;import Android.os.environment;public class FileService {private Context cont Ext Public Fileservice (Context context) {This.context = context; Public Fileservice () {} public String Getfilefromsdcard (string fileName) {FileInputStream InputStream = NULL; Cached streams, and disk-independent, do not need to close bytearrayoutputstream outputstream = new Bytearrayoutputstream (); File File = new file (Environment.getexternalstoragedirectory (), fileName); if (Environment.MEDIA_MOUNTED.equals (environment. Getexternalstoragestate ())) {try { InputStream = new FileInputStream (file); int len = 0; byte[] data = new byte[1024]; while (len = inputstream.read (data))! =-1) {outputstream.write (data, 0, Len); }} catch (FileNotFoundException e) {//TODO auto-generated catch block e.prints Tacktrace (); } catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); } finally {if (InputStream! = null) {try {inputstream.close ( ); } catch (IOException e) {//TODO auto-generated catch block E.printstacktra CE (); }}}} return new String (Outputstream.tobytearray ()); }/** * @param filename * The name of the file * @param content * File contents * @return */ public boolean Savecontenttosdcard (string fileName, String content) {Boolean flag = false; FileOutputStream fileoutputstream = null; Get the path to the sdcard card file File = new file (EnviRonment.getexternalstoragedirectory (), fileName); Determine if the SDcard card is available if (Environment.MEDIA_MOUNTED.equals (environment. Getexternalstoragestate ())) { try {fileoutputstream = new FileOutputStream (file); Fileoutputstream.write (Content.getbytes ()); Flag = true; } catch (FileNotFoundException e) {//TODO auto-generated catch block E.printstacktrace (); } catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); } finally {if (FileOutputStream! = null) {try {fil Eoutputstream.close (); } catch (IOException e) {//TODO auto-generated catch block E.printstacktra CE (); }}}} return flag; }}
Manifest file
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>