0

I am developing an Android Application for a Honeywell CK75 Scanner that saves barcode scans to an XML file stored on the device.

The app is being built for Android SDK level 19. The permissions for accessing the storage are in place

This is the code for the method that creates and saves the XML file.

public static void writeToXMLFile(List<Scan> scans, Context context) throws IOException {

        XmlSerializer serializer = Xml.newSerializer();
        StringWriter writer = new StringWriter();
        serializer.setOutput(writer);
        serializer.startDocument("UTF-8", true);
        serializer.startTag(null, "Scans");

        for(Scan s : scans){

            serializer.startTag(null, "Scan");

            serializer.startTag(null, "AimID");
            serializer.text(s.getAimID());
            serializer.endTag(null, "AimID");

            serializer.startTag(null, "BarcodeData");
            serializer.text(s.getBarcodeData());
            serializer.endTag(null, "BarcodeData");

            serializer.startTag(null, "BarcodeID");
            serializer.text(s.getBarcodeID());
            serializer.endTag(null, "BarcodeID");

            serializer.startTag(null, "TimeStamp");
            serializer.text(s.getTimestamp());
            serializer.endTag(null, "TimeStamp");

            if(s.getTrailerNo().equals(null) || s.getTrailerNo().isEmpty()){
                serializer.startTag(null, "BinNo");
                serializer.text(s.getBinNo());
                serializer.endTag(null, "BinNo");
            }
            else{
                serializer.startTag(null, "TrailerNo");
                serializer.text(s.getTrailerNo());
                serializer.startTag(null, "TrailerNo");
            }
        }


        serializer.endDocument();
        String result = writer.toString();

        String url = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/Scan.xml";
        File f = new File(url);
        f.getParentFile().mkdir();
        f.createNewFile();
        FileOutputStream output = new FileOutputStream(f, false);
        output.write(result.getBytes(),0,result.length());
        output.close();

    }

I expect that the file is save to the Documents folder, however when I check that folder, there is no XML file there.

Edit

It just seems like its not creating the file. I have tried several different methods of creating the file, and none have worked. At this point in time, I'm not concerned with whether or not the XML is okay, I am just trying to created a file on Android that can be accessed from a PC

  • I HIGHLY RECOMMEND USING SQLITE INSTEAD – asim Sep 10 '19 at 16:27
  • @user10844401 I'm using XML as once the device is plugged in to a computer, the XML file will be sent to a program that will then push that data into an existing database. Also, I can't do a direct connection to the database from the device as the location where it will be used does not have a sufficient internet connection – Zac Beatty-Taylor Sep 10 '19 at 16:31
  • I think sqlite data will be much easier to import into other databases. Also, you can export sqlite to xml or csv with ease. – asim Sep 10 '19 at 16:34
  • @user10844401 The problem isn't whether its XML, or sqlite. The problem is I can't create any type of file created on the device. – Zac Beatty-Taylor Sep 10 '19 at 16:42
  • Possible duplicate of [how to create xml file in android](https://stackoverflow.com/questions/5181294/how-to-create-xml-file-in-android) – Harsh Jatinder Sep 10 '19 at 18:15

1 Answers1

0

Solved. The issue is not that the file isn't being created. It is. The problem is when I went looking for the file in File Explorer, it doesn't appear. However, if I use the File Explorer built in to Android Studio, I can locate the file