简体   繁体   中英

Unable to create XML file on Android Device

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM