Downloaded files going missing in Android 4.4.2 -
Downloaded files going missing in Android 4.4.2 -
we using built-in downloadmanager
grab files our server. if figure out there has been update file delete local version , re-queue download downloadmanager
. runs when kill , re-start app (timely updates files not priority, have files , updated whenever notice it). scheme works on of personal testing devices, however, when testing in api 19 emulator or on co-worker's htc 1 files download , disappear (no longer in app's external info folder). i've figured out both version 4.4.2 of android (where devices either 4.4.4 or 4.0.4). it's weird because stick around time, random files disappear.
here code:
assetmanager setup (setup of output folder)
private assetmanager(context activity){ if(singleton != null&&ioutils.hasexternalstorage() != ioutils.extstoragestate_ok){ return; } context = activity; external = contextcompat.getexternalfilesdirs(context, "")[0]; external.mkdirs(); imagefolder = new file(external,imagepath); imagefolder.mkdirs(); singleton = this; }
download code
private static class downloadobject { public string serverid; public string updated_at; public uri image; public downloadobject() { super(); } public downloadobject(string serverid,string updated_at){ super(); this.serverid = serverid; this.updated_at = updated_at; } public downloadobject(cursor cursor){ super(); this.serverid = cursor.getstring(cursor.getcolumnindex(objectdao.properties.serverid.columnname)); this.updated_at = cursor.getstring(cursor.getcolumnindex(objectdao.properties.updatedat.columnname)); string imagefile = cursor.getstring(cursor.getcolumnindex(objectdao.properties.image.columnname)); this.image = uri.parse(assetmanager.getsingleton().getimagefolder().getpath()).buildupon().appendpath(imagefile).scheme("file").build(); } } //downloadobjectvector fresh list of objects server //existingobjects cursor db lists existing object locally private void spinoffdownloads(final vector<downloadobject> downloadobjectvector,final cursor existingobjects){ new thread(new runnable() { @override public void run() { int count = 0; if(existingobjects != null){ count = existingobjects.getcount(); } if (count>0){ existingobjects.movetofirst(); do{ final downloadobject obj = new downloadobject(existingobjects); downloadobject notneededobject = arrayutils.findfirst(downloadobjectvector,new arrayutils.predicate<downloadobject>() { @override public boolean evaluate(downloadobject downloadobject) { homecoming downloadobject.serverid.equals(obj.serverid)&&downloadobject.updated_at.compareto(obj.updated_at) <= 0; } }); if (notneededobject != null){ file imagetest = null; if(notneededobject.image != null) { uri out = uri.parse(assetmanager.getsingleton().getimagefolder().getpath()).buildupon().appendpath(notneededobject.image.getlastpathsegment()).scheme("file").build(); imagetest = new file(out.getpath()); }else{ log.v(class_name,"object null image:"+notneededobject.serverid); } if (imagetest == null||imagetest.exists()) { downloadobjectvector.remove(notneededobject); }else{ if (imagetest != null&&imagetest.exists()&&should_replace_file){ log.v(class_name,"deleting file(missing image):"+imagetest.getabsolutepath()); imagetest.delete(); } } }else{ file imagetest = null; if(obj.image != null) { imagetest = new file(obj.image.getpath()); if (imagetest != null&&imagetest.exists()&&should_replace_file){ log.v(class_name,"deleting file(image):"+imagetest.getabsolutepath()); imagetest.delete(); } }else{ log.v(class_name,"object null image:"+obj.serverid); } } }while(existingobjects.movetonext()); } if (existingobjects!= null){ try{ existingobjects.close(); }catch (exception e){ } } downloadmanager dm = (downloadmanager)getsystemservice(download_service); (int = 0; < downloadobjectvector.size(); i++) { seek { downloadobject dlobj = downloadobjectvector.get(i); uri in = dlobj.image; uri out = uri.parse(assetmanager.getsingleton().getimagefolder().getpath()).buildupon().appendpath(in.getlastpathsegment()).scheme("file").build(); dm.enqueue(new downloadmanager.request(in).setdestinationuri(out).settitle(in.getlastpathsegment())); }catch (exception e){ log.w(class_name,"error download queued:",e); } } } }).start(); }
please allow me know if need other info or code!
edit1
so decided elaborate on bit more testing , how issue manifests in hopes create image much more clear!
i start loading app via android studio , letting run long plenty know downloads finish , through app see images there , missing. images there normally. next exit app , utilize android task manager kill it. re-launch app via android studio. wait create sure downloads finish , watch logcat see files deleted manually(normally couple @ maximum). go through app see images still there/which have been added. seems every time new images appear , new images disappear... , ones marked manually deleted replaced via download properly(i.e. not "disappeared").
please allow me know if there tests me do!
file observer test
first of first time i've used fileobserver
if i've done stupid please point out. here observer code:
external = contextcompat.getexternalfilesdirs(context, null)[0]; external.mkdirs(); fileobserver = new fileobserver(external.getpath(),fileobserver.all_events) { @override public void onevent(final int event, final string relpath) { string msg = "???"; switch (event){ case fileobserver.delete: msg = "fileob delete relpath:"+relpath; break; case fileobserver.delete_self: msg = "fileob delete_self relpath:"+relpath; break; case fileobserver.modify: msg = "fileob modify relpath:"+relpath; break; case fileobserver.move_self: msg = "fileob move_self relpath:"+relpath; break; case fileobserver.moved_to: msg = "fileob moved_to relpath:"+relpath; break; case fileobserver.moved_from: msg = "fileob moved_from relpath:"+relpath; break; case fileobserver.attrib: msg = "fileob attrib relpath:"+relpath; break; case fileobserver.create: msg = "fileob create relpath:"+relpath; break; default: msg = "unknown event:"+event+" @ relpath:"+relpath; } fileobserverhandler.publish(new logrecord(level.info,msg)); fileobserverhandler.flush(); } @override public void startwatching() { super.startwatching(); fileobserverhandler.publish(new logrecord(level.info,"start watching!!!!")); fileobserverhandler.flush(); log.v("fileobserver","start watching!!!"); } }; fileobserver.startwatching();
i'm using handler because @ first didn't have startwatching()
override in , wasn't getting logging @ , docs onevent happens on own thread , hence should utilize handler. it's in class:
public static handler fileobserverhandler = new consolehandler();
the output @ "start watching!!!". i'm guessing must have done wrong, because see downloading/deleting things... @ to the lowest degree says is.
the behavior describe sounds scheme clearing files cache.
in phone call getexternalfilesdirs
utilize ""
, trying creating file
/directory ""
can problematic.
use null
instead of ""
in phone call getexternalfilesdirs
see if helps
replace
external = contextcompat.getexternalfilesdirs(context, "")[0];
with
external = contextcompat.getexternalfilesdirs(context, null)[0];
android file android-download-manager android-externalstorage
Comments
Post a Comment