0

我正在尝试从 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 资源管理器查看并打开它。

任何人的想法?

4

1 回答 1

0

尝试使用此代码,

public void DownloadFromUrl(String DownloadUrl, String fileName) {

   try {
           File root = android.os.Environment.getExternalStorageDirectory();               

           File dir = new File (root.getAbsolutePath() + "/xmls");
           if(dir.exists()==false) {
                dir.mkdirs();
           }

           URL url = new URL(DownloadUrl); //you can write here any link
           File file = new File(dir, fileName);

           long startTime = System.currentTimeMillis();
           Log.d("DownloadManager", "download begining");
           Log.d("DownloadManager", "download url:" + url);
           Log.d("DownloadManager", "downloaded file name:" + fileName);

           /* Open a connection to that URL. */
           URLConnection ucon = url.openConnection();

           /*
            * Define InputStreams to read from the URLConnection.
            */
           InputStream is = ucon.getInputStream();
           BufferedInputStream bis = new BufferedInputStream(is);

           /*
            * Read bytes to the Buffer until there is nothing more to read(-1).
            */
           ByteArrayBuffer baf = new ByteArrayBuffer(5000);
           int current = 0;
           while ((current = bis.read()) != -1) {
              baf.append((byte) current);
           }


           /* Convert the Bytes read to a String. */
           FileOutputStream fos = new FileOutputStream(file);
           fos.write(baf.toByteArray());
           fos.flush();
           fos.close();
           Log.d("DownloadManager", "download ready in" + ((System.currentTimeMillis() - startTime) / 1000) + " sec");

   } catch (IOException e) {
       Log.d("DownloadManager", "Error: " + e);
   }

}
于 2012-09-28T08:48:58.837 回答