Save XML file on Android device and read it

拟墨画扇 提交于 2020-03-06 02:18:30

问题


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

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