Android copy from assets to files directory -
hi have a collection of json files in assets/data/ folder on launch want check see if files exists in internal files directory if of them dont exist want copy them assets/data/ folder internal storage files directory. @ moment i'm detecting if files exist or not struggling copy them assets. know how achieve this?
heres have tried far
public static void loaddata(context context){ keydata[] = {"1","2","3","4","5","6"} jsonarray exists = new jsonarray(); log.v("data", "keydata = " + keydata); log.v("data", "keydata.length = " + keydata.length); (string directory : keydata ) { file file = new file(directory); if(file.exists() && file.isdirectory()){ exists.put("true"); } else { exists.put("false"); } } log.v("data", "exists = " + exists); if(exists.tostring().contains("false")){ for(int i=0; < keydata.length; i++){ string filename = keydata[i]; copyfilefromassetstointernalstorage(context,filename); } } } public static void copyfilefromassetstointernalstorage(context context,string filename){ string destinationfile = context.getfilesdir().getpath() + file.separator + filename; if (!new file(destinationfile).exists()) { try { copyfromassetstostorage(context, "data/" + filename, destinationfile); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } } private static void copyfromassetstostorage(context context, string sourcefile, string destinationfile) throws ioexception { inputstream = context.getassets().open(sourcefile); outputstream os = new fileoutputstream(destinationfile); copystream(is, os); os.flush(); os.close(); is.close(); } private static void copystream(inputstream input, outputstream output) throws ioexception { byte[] buffer = new byte[5120]; int length = input.read(buffer); while (length > 0) { output.write(buffer, 0, length); length = input.read(buffer); } } public final static void writedatatofile(context activitycontext, string writablestring, string filename){ fileoutputstream fos=null; try { fos=activitycontext.openfileoutput(filename, 0); activitycontext.getfilesdir(); fos.write(writablestring.getbytes()); } catch (filenotfoundexception e) { log.e("createfile", e.getlocalizedmessage()); } catch (ioexception e) { log.e("createfile", e.getlocalizedmessage()); } finally{ if(fos!=null){ try { // drain stream fos.flush(); fos.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } } }
Comments
Post a Comment