java - How to write files to external public storage in Android so that they are visible from Windows? -
my app should save files place where, when connect phone/tablet computer, can see them through system file explorer.
this way implemented file writing:
protected string mdir = environment.directory_documents; protected file mpath = environment.getexternalstoragepublicdirectory(mdir); protected void writelogfile(string filename) { file f = new file(mpath, filename + ".txt"); f.getparentfile().mkdirs(); try (bufferedwriter bw = new bufferedwriter(new filewriter(f, false))) { // details omitted. } catch (exception e) { e.printstacktrace(); return; } maketext("wrote " + f.getabsolutepath()); }
this see when connect sony xperia z4 tablet windows (notice missing documents folder):
this directory file written (using above implementation):
what wrong implementation?
what wrong implementation?
mediastore
has not discovered newly-created files yet. see in windows — , in many on-device "gallery" apps — based on mediastore
has indexed.
use mediascannerconnection
, scanfile()
method tell mediastore
file, once have written out data disk:
public void scanfile(context ctxt, file f, string mimetype) { mediascannerconnection .scanfile(ctxt, new string[] {f.getabsolutepath()}, new string[] {mimetype}, null); }
Comments
Post a Comment