I have a problem with FAT12 file system support. I'm trying to read/write 
XML file on 2Mb memory device. This device is formatted in FAT12. In a good 
case first, I found a path by using broadcast receiver when I plug my 
android device and then create file like this:

public ConfigToXMLTask(String filename, Context context) {
 this.file = new File(filename);
 this.context = context;
}

@Override
protected File doInBackground(MainConfig... params){
  DocumentBuilderFactory xmlFactory = DocumentBuilderFactory.newInstance();
  DocumentBuilder xmlBuilder = xmlFactory.newDocumentBuilder();
  Document doc = xmlBuilder.newDocument();
  Element rootElement = doc.createElement("document");
  rootElement.setAttribute("ppk", config.getDeviceName());
  rootElement.setAttribute("ver", "01");
  rootElement.setAttribute("rev", "00");
  doc.appendChild(rootElement);
  config.buildXMLElement(rootElement);
  Transformer transformer = TransformerFactory.newInstance().newTransformer();
  transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "yes");
  transformer.setOutputProperty(OutputKeys.ENCODING, "Windows-1251");
  transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount";, 
"2");
  file.createNewFile();
  transformer.transform(new DOMSource(doc), new StreamResult(new 
FileOutputStream(file)));
}

The problem is that on some android devices access to my 2Mb flash drive not 
available and broadcast receiver doesn't work.
Here is a code of receiver:


private UsbReceiver receiver;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        receiver = new UsbReceiver();
        filter = new IntentFilter();
        filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
        filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
        filter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
        filter.addDataScheme("file");
        registerReceiver(receiver, filter);
    }

public static class UsbReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(final Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_MEDIA_MOUNTED)) {
                Uri uriFile = intent.getData();
                File dir = new File(uriFile.getPath());
                SharedPreferences prefs = 
PreferenceManager.getDefaultSharedPreferences(context);
                prefs.edit().putString("usb", uriFile.getPath()).apply();
                if (dir.canRead()) {
                    String[] fileList = dir.list();
                    for (String aFileList : fileList) {
                        if (aFileList.contains("CONFIG.XML")) {
                            final File config = new File(dir, aFileList);
                            prefs.edit().putString("dir", 
dir.getPath()).apply();
                            new AlertDialog.Builder(context)
                                    .setTitle(R.string.attention)
                                    .setMessage(R.string.device_detected)
                                    .setPositiveButton("OK", new 
DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface 
dialog, int which) {
                                            
MyXMLParser.convertXMLtoConfig(config, context);
                                        }
                                    })
                                    .setNegativeButton(android.R.string.cancel, 
null)
                                    .show();
                        }
                    }
                }
            }
        }
    }


@Override
    protected void onResume() {
        registerReceiver(receiver, filter);
        wheel.startAnimation(wheel.getAnimation());
        super.onResume();
    }

    @Override
    protected void onPause() {
        unregisterReceiver(receiver);
        super.onPause();
    }


Default file managers can read files, but I haven't found good solutions for 
reading (or receiving access to write/reading) files from USB OTG.
Maybe there is some library based on  documentation 
https://developer.android.com/guide/topics/connectivity/usb/host.html for FAT12?

Please give me an answer or a link to the source in which there is a 
description or examples.

-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/3d83a5ad-906e-4277-824f-94976c1290b9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to