/*** Save JSON to local *@parammactivity *@paramfilename *@paramcontent*/ Public StaticFile dir =NewFile (Environment.getexternalstoragedirectory () + "/. imageloader/json/"); Public Static voidSavetosdcard (Activity mactivity, string filename, string content) {string en=environment.getexternalstoragestate (); //gets the SDcard status if SDcard is plugged into a phone and is not write protected if(En.equals (environment.media_mounted)) {Try{dir.mkdirs ();//Create folders where write filesFile File =NewFile (dir, filename); OutputStream out=Newfileoutputstream (file); Out.write (Content.getbytes ()); Out.close ();Apputils.showtoast (mactivity, "save Success");}Catch(Exception e) {e.printstacktrace ();Apputils.showtoast (mactivity, "Save Failed"); } } Else { //prompt for user sdcard not present or write protected statusApputils.showtoast (mactivity, "sdcard not present or write protected State"); } } /*** Read JSON from the local *@parammactivity *@paramfilename *@paramcontent*/ Public Staticstring ReadTextFile (String filePath) {StringBuilder sb=NewStringBuilder (); Try{File File=NewFile (dir + "/" +FilePath); InputStream in=NULL; Inch=Newfileinputstream (file); intTempbyte; while((Tempbyte = In.read ())! =-1) {sb.append (Char) tempbyte); } in.close (); } Catch(Exception e) {e.printstacktrace (); } returnsb.tostring (); }
Android store JSON to local, and read local JSON