我正在尝试从 URL 下载 xml 文件,将其保存到内部 sd 卡并解析 xml。这是我的下载功能:
private static String Name = project_name.getText().toString();
private static String URL = url.getText().toString();
//Download XML file from URL
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(URL));
request.setTitle("Download von "+Name+".xml");
request.setDestinationInExternalPublicDir("/", Name+".xml");
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
private static ArrayList<Datapoint> itemsList = parseXML();
这是解析器函数:
private ArrayList<Datapoint> parseXML() {
try {
Log.w("AndroidParseXMLActivity", "Start");
/** Handling XML */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
File file = new File(Environment.getExternalStorageDirectory()+"/"+ Name+ ".xml");
XMLContentHandler myXMLHandler = new XMLContentHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(new InputStreamReader(new FileInputStream(file))));
itemsList = myXMLHandler.getItemsList();
Log.w("AndroidParseXMLActivity", "Done");
}
catch (Exception e) {
Log.w("AndroidParseXMLActivity",e );
}
return itemsList ;
}
但我收到以下 Logcat 错误:
09-28 09:30:36.500: W/AndroidParseXMLActivity(7881): Start
09-28 09:30:36.540: W/AndroidParseXMLActivity(7881): java.io.FileNotFoundException: /mnt/sdcard/mich.xml: open failed: ENOENT (No such file or directory)
09-28 09:30:36.540: W/AndroidParseXMLActivity(7881): at libcore.io.IoBridge.open(IoBridge.java:406)
09-28 09:30:36.540: W/AndroidParseXMLActivity(7881): at java.io.FileInputStream.<init>(FileInputStream.java:78)
09-28 09:30:36.540: W/AndroidParseXMLActivity(7881): at de.myapp.CreateProject.parseXML(CreateProject.java:348)
09-28 09:30:36.540: W/AndroidParseXMLActivity(7881): at de.myapp.CreateProject.save_project(CreateProject.java:298)
09-28 09:30:36.540: W/AndroidParseXMLActivity(7881): at de.myapp.CreateProject.onOptionsItemSelected(CreateProject.java:115)
09-28 09:30:36.540: W/AndroidParseXMLActivity(7881): at android.app.Activity.onMenuItemSelected(Activity.java:2564)
09-28 09:30:36.540: W/AndroidParseXMLActivity(7881): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:984)
09-28 09:30:36.540: W/AndroidParseXMLActivity(7881): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
09-28 09:30:36.540: W/AndroidParseXMLActivity(7881): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
09-28 09:30:36.540: W/AndroidParseXMLActivity(7881): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
09-28 09:30:36.540: W/AndroidParseXMLActivity(7881): at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:613)
09-28 09:30:36.540: W/AndroidParseXMLActivity(7881): at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:121)
09-28 09:30:36.540: W/AndroidParseXMLActivity(7881): at android.view.View.performClick(View.java:3644)
09-28 09:30:36.540: W/AndroidParseXMLActivity(7881): at android.view.View$PerformClick.run(View.java:14313)
09-28 09:30:36.540: W/AndroidParseXMLActivity(7881): at android.os.Handler.handleCallback(Handler.java:605)
09-28 09:30:36.540: W/AndroidParseXMLActivity(7881): at android.os.Handler.dispatchMessage(Handler.java:92)
09-28 09:30:36.540: W/AndroidParseXMLActivity(7881): at android.os.Looper.loop(Looper.java:137)
09-28 09:30:36.540: W/AndroidParseXMLActivity(7881): at android.app.ActivityThread.main(ActivityThread.java:4517)
09-28 09:30:36.540: W/AndroidParseXMLActivity(7881): at java.lang.reflect.Method.invokeNative(Native Method)
09-28 09:30:36.540: W/AndroidParseXMLActivity(7881): at java.lang.reflect.Method.invoke(Method.java:511)
09-28 09:30:36.540: W/AndroidParseXMLActivity(7881): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
09-28 09:30:36.540: W/AndroidParseXMLActivity(7881): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
09-28 09:30:36.540: W/AndroidParseXMLActivity(7881): at dalvik.system.NativeStart.main(Native Method)
09-28 09:30:36.540: W/AndroidParseXMLActivity(7881): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
09-28 09:30:36.540: W/AndroidParseXMLActivity(7881): at libcore.io.Posix.open(Native Method)
09-28 09:30:36.540: W/AndroidParseXMLActivity(7881): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
09-28 09:30:36.540: W/AndroidParseXMLActivity(7881): at libcore.io.IoBridge.open(IoBridge.java:390)
09-28 09:30:36.540: W/AndroidParseXMLActivity(7881): ... 22 more
09-28 09:30:36.590: E/SKIA(7881): FimgApiStretch:stretch failed
09-28 09:30:39.350: D/AbsListView(7881): Get MotionRecognitionManager
09-28 09:30:39.435: D/dalvikvm(7881): GC_CONCURRENT freed 238K, 5% free 15465K/16135K, paused 1ms+4ms
09-28 09:30:39.440: D/AbsListView(7881): [unregisterDoubleTapMotionListener]
09-28 09:30:39.440: I/MotionRecognitionManager(7881): .unregisterListener : / listener count = 0->0, listener=android.widget.AbsListView$4@41a3a5e0
09-28 09:30:40.675: D/FILESTREAM(7881): Filepath:/data/data/de.myapp/files/mich
09-28 09:30:40.675: D/AndroidRuntime(7881): Shutting down VM
09-28 09:30:40.675: W/dalvikvm(7881): threadid=1: thread exiting with uncaught exception (group=0x40c621f8)
09-28 09:30:40.680: E/AndroidRuntime(7881): FATAL EXCEPTION: main
09-28 09:30:40.680: E/AndroidRuntime(7881): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.myapp/myapp.ProjectView}: java.lang.NullPointerException
09-28 09:30:40.680: E/AndroidRuntime(7881): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
09-28 09:30:40.680: E/AndroidRuntime(7881): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
09-28 09:30:40.680: E/AndroidRuntime(7881): at android.app.ActivityThread.access$600(ActivityThread.java:128)
09-28 09:30:40.680: E/AndroidRuntime(7881): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
09-28 09:30:40.680: E/AndroidRuntime(7881): at android.os.Handler.dispatchMessage(Handler.java:99)
09-28 09:30:40.680: E/AndroidRuntime(7881): at android.os.Looper.loop(Looper.java:137)
09-28 09:30:40.680: E/AndroidRuntime(7881): at android.app.ActivityThread.main(ActivityThread.java:4517)
09-28 09:30:40.680: E/AndroidRuntime(7881): at java.lang.reflect.Method.invokeNative(Native Method)
09-28 09:30:40.680: E/AndroidRuntime(7881): at java.lang.reflect.Method.invoke(Method.java:511)
09-28 09:30:40.680: E/AndroidRuntime(7881): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
09-28 09:30:40.680: E/AndroidRuntime(7881): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
09-28 09:30:40.680: E/AndroidRuntime(7881): at dalvik.system.NativeStart.main(Native Method)
09-28 09:30:40.680: E/AndroidRuntime(7881): Caused by: java.lang.NullPointerException
09-28 09:30:40.680: E/AndroidRuntime(7881): at de.myapp.ProjectView.setup(ProjectView.java:148)
09-28 09:30:40.680: E/AndroidRuntime(7881): at de.myapp.ProjectView.onCreate(ProjectView.java:62)
09-28 09:30:40.680: E/AndroidRuntime(7881): at android.app.Activity.performCreate(Activity.java:4533)
09-28 09:30:40.680: E/AndroidRuntime(7881): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
09-28 09:30:40.680: E/AndroidRuntime(7881): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
09-28 09:30:40.680: E/AndroidRuntime(7881): ... 11 more
我还在我的 AndroidManifest.xml 中设置了此权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
</uses-permission>
xml 已下载并保存到 sd 卡,因为我可以使用 Windows 资源管理器查看并打开它。
任何人的想法?