问题
I have issues with storing and reading an XML file on my Acer Iconia Tab A 100.
The steps I followed are:
1) Plug in the tab to my system.
2) Copy test.xml file from my laptop to Acer Iconia Tab/ SDCard/ mydir/test.xml
3) Try to open this file in Android code, as follows:
File testFile = null;
File dir = new File(android.os.Environment.getExternalStorageDirectory(),"mydir");
if(dir.exists())
testFile = new File(dir, "test.xml");
if (testFile != null)
Log.d ("File length="+testFile.length()); // returns 0
When I execute the above code, it prints out the length as 0. I have tried other combination as follows, but in vain:
File dir = new File("/data","mydir");
File dir = new File("/mnt/sdcard","mydir");
Can someone please help?
Thanks. Ani
回答1:
File length
public long length()
Returns the length of the file denoted by this abstract pathname. The return value is unspecified if this pathname denotes a directory.
Returns: The length, in bytes, of the file denoted by this abstract pathname, or 0L if the file does not exist
So just check it, whether file exist or not,
By using File.exist();
Also, By looking in File Explorer -> DDMS via eclipse find the exact path of the file. It also show size of the file.
来源:https://stackoverflow.com/questions/8337602/save-xml-file-on-android-device-and-read-it