Problems saving a xml file in Android -
i trying save array list xml file in internal storage using xmlserializer. app scans ble , when stop button pressed should save parameters list of ble has detected. runs perfect , when pressed stop button stops scan , displays in mobile screen toast message xml file has been created successfully. however, when seach xml file, doesn´t appear anywhere if hasn´t been created. don´t know reason of problem. here code have implemented:
package com.example.newblescan; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.io.objectoutputstream; import java.io.serializable; import org.xmlpull.v1.xmlserializer; import com.example.newblescan.r; import android.app.activity; import android.app.listactivity; import android.bluetooth.bluetoothadapter; import android.bluetooth.bluetoothdevice; import android.bluetooth.bluetoothmanager; import android.content.context; import android.content.intent; import android.content.pm.packagemanager; import android.os.bundle; import android.text.format.dateformat; import android.util.xml; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.listview; import android.widget.toast; import com.example.newblescan.adapter.bledevicesadapter; /** * activity scanning , displaying available bluetooth le devices. */ public class devicescanactivity extends listactivity { private static final int request_enable_bt = 1; private static final long scan_period = 100; private bledevicesadapter ledevicelistadapter; private bluetoothadapter bluetoothadapter; private scanner scanner; private save save; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); getactionbar().settitle(r.string.title_devices); // use check determine whether ble supported on device. can // selectively disable ble-related features. if (!getpackagemanager().hassystemfeature(packagemanager.feature_bluetooth_le)) { toast.maketext(this, r.string.ble_not_supported, toast.length_short).show(); finish(); return; } // initializes bluetooth adapter. api level 18 , above, reference // bluetoothadapter through bluetoothmanager. final bluetoothmanager bluetoothmanager = (bluetoothmanager) getsystemservice(context.bluetooth_service); bluetoothadapter = bluetoothmanager.getadapter(); // checks if bluetooth supported on device. if (bluetoothadapter == null) { toast.maketext(this, r.string.error_bluetooth_not_supported, toast.length_short).show(); finish(); return; } } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.gatt_scan, menu); if (scanner == null || !scanner.isscanning()) { menu.finditem(r.id.menu_stop).setvisible(false); menu.finditem(r.id.menu_scan).setvisible(true); menu.finditem(r.id.menu_refresh).setactionview(null); } else { menu.finditem(r.id.menu_stop).setvisible(true); menu.finditem(r.id.menu_scan).setvisible(false); menu.finditem(r.id.menu_refresh).setactionview( r.layout.actionbar_indeterminate_progress); } return true; } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case r.id.menu_scan: ledevicelistadapter.clear(); if (scanner == null) { scanner = new scanner(bluetoothadapter, mlescancallback); scanner.startscanning(); invalidateoptionsmenu(); } break; case r.id.menu_stop: if (scanner != null) { save = new save(ledevicelistadapter); scanner.stopscanning(); try { save.savedata(); } catch (filenotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } scanner = null; invalidateoptionsmenu(); } break; } return true; } @override protected void onresume() { super.onresume(); // ensures bluetooth enabled on device. if bluetooth not enabled, // fire intent display dialog asking user grant permission enable it. if (!bluetoothadapter.isenabled()) { final intent enablebtintent = new intent(bluetoothadapter.action_request_enable); startactivityforresult(enablebtintent, request_enable_bt); return; } init(); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { // user chose not enable bluetooth. if (requestcode == request_enable_bt) { if (resultcode == activity.result_canceled) { finish(); } else { init(); } } super.onactivityresult(requestcode, resultcode, data); } @override protected void onpause() { super.onpause(); if (scanner != null) { scanner.stopscanning(); scanner = null; } } @override protected void onlistitemclick(listview l, view v, int position, long id) { final bluetoothdevice device = ledevicelistadapter.getdevice(position); if (device == null) return; //final intent intent = new intent(this, deviceservicesactivity.class); //intent.putextra(deviceservicesactivity.extras_device_name, device.getname()); //intent.putextra(deviceservicesactivity.extras_device_address, device.getaddress()); //startactivity(intent); } private void init() { if (ledevicelistadapter == null) { ledevicelistadapter = new bledevicesadapter(getbasecontext()); setlistadapter(ledevicelistadapter); } if (scanner == null) { scanner = new scanner(bluetoothadapter, mlescancallback); scanner.startscanning(); } invalidateoptionsmenu(); } // device scan callback. private bluetoothadapter.lescancallback mlescancallback = new bluetoothadapter.lescancallback() { @override public void onlescan(final bluetoothdevice device, final int rssi, byte[] scanrecord) { runonuithread(new runnable() { @override public void run() { ledevicelistadapter.adddevice(device, rssi); ledevicelistadapter.notifydatasetchanged(); } }); } }; private static class scanner extends thread { private final bluetoothadapter bluetoothadapter; private final bluetoothadapter.lescancallback mlescancallback; private volatile boolean isscanning = false; scanner(bluetoothadapter adapter, bluetoothadapter.lescancallback callback) { bluetoothadapter = adapter; mlescancallback = callback; } public boolean isscanning() { return isscanning; } public void startscanning() { synchronized (this) { isscanning = true; start(); } } public void stopscanning() { synchronized (this) { isscanning = false; bluetoothadapter.stoplescan(mlescancallback); } } @override public void run() { try { while (true) { synchronized (this) { if (!isscanning) break; bluetoothadapter.startlescan(mlescancallback); } sleep(scan_period); synchronized (this) { bluetoothadapter.stoplescan(mlescancallback); } } } catch (interruptedexception ignore) { } { bluetoothadapter.stoplescan(mlescancallback); } } } public class save { /** * */ private bledevicesadapter ledevicelistadapter; save(bledevicesadapter blelist) { ledevicelistadapter = blelist; } public void savedata() throws filenotfoundexception{ string filename = "dragon.txt"; //string date = (dateformat.format("dd-mm-yyyy hh:mm:ss", new java.util.date()).tostring()); fileoutputstream fos = null; int size = ledevicelistadapter.getcount(); //bundle extras = getintent().getextras(); //long timestamp = extras.getlong("currenttime"); try { fos= openfileoutput(filename, context.mode_private); xmlserializer serializer = xml.newserializer(); serializer.setoutput(fos, "utf-8"); serializer.startdocument(null, boolean.valueof(true)); serializer.setfeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); serializer.starttag("", "root"); //serializer.starttag("", "timestamp"); //serializer.text(date); //serializer.endtag("", "timestamp"); for(int j = 0 ; j < size ; j++) { bluetoothdevice devices = ledevicelistadapter.getdevice(j); //integer signal = ledevicelistadapter.getrssi(j); serializer.starttag("", "name"); serializer.text(devices.getname()); serializer.endtag("", "name"); serializer.starttag("", "address"); serializer.text(devices.getaddress()); serializer.endtag("", "address"); //serializer.starttag(null, "rssi"); //serializer.setproperty(null, signal); //serializer.endtag(null, "rssi"); } //objectoutputstream out = new objectoutputstream(fos); //out.write((int) timestamp); //out.writeobject(ledevicelistadapter); //out.close(); serializer.enddocument(); serializer.flush(); fos.close(); toast.maketext(devicescanactivity.this, r.string.list_saved, toast.length_short).show(); } catch (filenotfoundexception e){ e.printstacktrace(); } catch (ioexception e){ e.printstacktrace(); } } } }
in class save, implement method savedata save list xml file.
does know problem? pleasee help!!!!
openfileoutput puts in /data/data/your_packagename/files directory. directory can read root , app- not able find file in other program without rooting device. not able find in file explorer unless have root access. if want able see file, need write somewhere world accessible, such sd card.
Comments
Post a Comment