Android FileWriter not working on Sony Ericsson Arc but works on another phone

a 夏天 提交于 2020-01-07 09:15:23

问题


The issue I am having could be a hardware-related. In any case I'm stumped.

I have written some code (that I took and modified from: Writing Text File to SD Card fails) I've put the code below. It works fine on my Sony Ericcson X8. However, on the Sony Ericsson Arc, I can't find the file when I look for it on the phone! I went line by line through the code and there are no failures. It's as if it's on the phone and I'm just blind. I can even see in the debugger that the value of gpxfile is:

/mnt/sdcard/MyCompany/MyLog But when I use windows explorer to look for the file, I certainly don't see the directory MyCompany. Is there some setting on the phone that (silently) prevents writing to the SD Card?

Here is the code:

public static boolean generateNoteOnSD(String sFileName, String sBody) {
    try {
        String auxSDCardStatus = Environment.getExternalStorageState() ;
        if (!auxSDCardStatus.equals(Environment.MEDIA_MOUNTED)) {
            Log.i(TAG, "generateNoteOnSD auxSDCardSTatus: " + auxSDCardStatus);
        }

        if (auxSDCardStatus.equals(Environment.MEDIA_MOUNTED)) {
            File root = new File(Environment.getExternalStorageDirectory(), "Dexcom");
            if (!root.exists()) {
                root.mkdirs();
            }
            File gpxfile = new File(root, sFileName);
            FileWriter writer = new FileWriter(gpxfile, true);
            String currentTimeString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());

            writer.append(currentTimeString + ", " + sBody +System.getProperty("line.separator") );

           //writer.newLine();
            writer.flush();
            writer.close();
            Log.d(TAG,"generateNoteOnSD: Saved to file: " + sBody);
            return true;
        } else {
            return false;
        }
    } catch(IOException e) {
         e.printStackTrace();
         return false;
         //importError = e.getMessage();
         //iError();
    }

}


回答1:


In my case the problem was that I had the Xperia ARC attached to the laptop with a USB cable. Apparently, things don't work quite right if you do that. No problem with the X8, so I'm guessing that it's phone specific. Computer may be putting lock on the file thus preventing Android from updating file. Not sure why I don't get an error though. Bottom line for future readers: Try disconnecting phone from computer.



来源:https://stackoverflow.com/questions/11213686/android-filewriter-not-working-on-sony-ericsson-arc-but-works-on-another-phone

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!