android - Need to find the location of .txt file in the root SD card -
i attempting write longitude , latitude values text file each time location changes. result of should text file, stored on sd card, contains list of longitude , latitude values. application gets longitude , latitude , toast notification pops saying file has been saved successfully. however, cannot find text file in root of sd card.
here code:
import java.io.file; import java.io.fileoutputstream; import java.io.outputstreamwriter; import android.location.location; import android.location.locationlistener; import android.location.locationmanager; import android.os.bundle; import android.app.activity; import android.content.context; import android.widget.textview; import android.widget.toast; public class mainactivity extends activity { textview textlat; textview textlong; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); textlat = (textview)findviewbyid(r.id.textlat); textlong = (textview)findviewbyid(r.id.textlong); locationmanager lm = (locationmanager)getsystemservice(context.location_service); locationlistener ll = new mylocationlistener(); lm.requestlocationupdates(locationmanager.gps_provider, 10000, 0, ll); } private class mylocationlistener implements locationlistener { @override public void onlocationchanged(location location) { if (location != null) { double plong = location.getlongitude(); double plat = location.getlatitude(); textlat.settext(double.tostring(plat)); textlong.settext(double.tostring(plong)); try { file myfile = new file("/sdcard/mysdfile.txt"); myfile.createnewfile(); fileoutputstream fout = new fileoutputstream(myfile); outputstreamwriter myoutwriter = new outputstreamwriter(fout); myoutwriter.append(textlat.gettext()); myoutwriter.close(); fout.close(); toast.maketext(getbasecontext(), "done writing sd 'mysdfile.txt'", toast.length_short).show(); } catch (exception e) { toast.maketext(getbasecontext(), e.getmessage(), toast.length_short).show(); } } } @override public void onproviderdisabled(string provider) { } @override public void onproviderenabled(string provider) { } @override public void onstatuschanged(string provider, int status, bundle extras) { } } }
so basically, file location values? feel i'm missing obvious here...
you should not try write root folder of sdcard. fail due security issues. try instead:
file dir = environment.getexternalstoragepublicdirectory(); file myfile = new file(dir, "mysdfile.txt");
you can later find file in directory dir
. if want file private app, use context.getexternalfilesdir()
instead of environment.getexternalstoragepublicdirectory()
.
also check out guide topic on storage options
Comments
Post a Comment