2013-09-23 269 views
0

我的应用程序使用jsoup lib从网上下载数据。而下载它的演出则不是找到这样的文件或目录。我使用CMD手动制作了SD卡。但仍然是错误。不知道如何解决这个问题。在模拟器中找不到SD卡

线程代码

final ProgressDialog progressDialog = new ProgressDialog(Edit.this); 
      progressDialog.show(); 
      progressDialog.setCancelable(false); 
      new Thread(new Runnable() { 
       @Override 
       public void run() { 
        try{ 
        new WebCopier.Copier(url, Environment.getExternalStorageDirectory().getAbsolutePath() + "/OfflineBrowser/" + title, 
          maxDepth, bDownloadImages, bDownloadAudio, bDownloadVideo).run(); 
        progressDialog.dismiss(); 
        } 
        catch (Exception e) 
        { 
         fn_ShowMsg("Error 1:" +e.toString()); 
        } 
       } 
      }).start(); 
     } 

复印机代码

public static void save (String filename, String url) throws Exception 
{ 
    org.jsoup.Connection conn = Jsoup.connect(url); 
    conn.timeout(60000); 
    conn.userAgent("Mozilla/5.0 (Linux; U; Android 2.3.5; en-us; HTC Vision Build/GRI40) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1"); 
    conn.ignoreContentType(true); 
    byte[] data = conn.execute().bodyAsBytes(); 
    save(filename, data); 
} 

public static void save (String filename, byte[] data) throws Exception 
{ 
    print("Saving file %s", filename); 
    if (filename.startsWith("file://")){ 
     URL url = new URL(filename); 
     filename = url.getPath(); 
    } 

    FileOutputStream os = new FileOutputStream(filename); 
    os.write(data); 
    os.close(); 
} 

错误

09-23 14:22:{35.377: DEBUG/Output(345): * link <https://play.google.com/store?hl=da> (alternate) 
09-23 14:22:38.026: DEBUG/dalvikvm(345): GC_FOR_MALLOC freed 1549K, 42% free 7714K/13127K, external 929K/1038K, paused 246ms 
09-23 14:22:38.115: DEBUG/Output(345): Saving file file:///file:/mnt/sdcard/OfflineBrowser/this /1oujn635h728x/lp1wij6q2wek 
09-23 14:22:38.126: WARN/System.err(345): java.io.FileNotFoundException: /file:/mnt/sdcard/OfflineBrowser/this /1oujn635h728x/lp1wij6q2wek (No such file or directory) 
09-23 14:22:38.136: WARN/System.err(345): at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method) 
09-23 14:22:38.146: WARN/System.err(345): at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232) 
09-23 14:22:38.146: WARN/System.err(345): at java.io.FileOutputStream.<init>(FileOutputStream.java:94) 
09-23 14:22:38.146: WARN/System.err(345): at java.io.FileOutputStream.<init>(FileOutputStream.java:165) 
09-23 14:22:38.156: WARN/System.err(345): at java.io.FileOutputStream.<init>(FileOutputStream.java:144) 
09-23 14:22:38.156: WARN/System.err(345): at com.example.offlinebrowser.WebCopier.save(WebCopier.java:431) 
09-23 14:22:38.156: WARN/System.err(345): at com.example.offlinebrowser.WebCopier.save(WebCopier.java:420) 
09-23 14:22:38.167: WARN/System.err(345): at com.example.offlinebrowser.WebCopier$Copier.run(WebCopier.java:178) 
09-23 14:22:38.176: WARN/System.err(345): at com.example.offlinebrowser.WebCopier$Copier.run(WebCopier.java:232) 
09-23 14:22:38.176: WARN/System.err(345): at com.example.offlinebrowser.Edit$2$1.run(Edit.java:75) 
09-23 14:22:38.186: WARN/System.err(345): at java.lang.Thread.run(Thread.java:1019) 

希望有人可以帮我请..

回答

2

您需要检查是否正在使用的目录已经创建..

if (filename.startsWith("file://")){ 
    URL url = new URL(filename); 
    filename = url.getPath(); 
    File dir = new File(filename); 
    if (!dir.exists()) // not exist? make it! 
     dir.mkdirs(); 
} 
+0

仍表现出同样的错误,如果你需要更多的信息相关的复印机代码,那么我可以告诉你.... @Nermeen –

+0

你确定路径是正确的,有一个空间后(this)(/ file:/ mnt/sdcard/OfflineBrowser/this/1oujn635h728x/lp1wij6q2 wek(没有这样的文件或目录)) – Nermeen

+0

他们没有错。看这个。 09-23 15:59:58.406:DEBUG /输出(426):*脚本: 09-23 16:00:00.949:DEBUG /输出(426):保存文件file:/// file:/ mnt/sdcard/OfflineBrowser/tit /hs29y05gei32/7mfm99n7it75.js 09-23 16:00:00.956:WARN/System.err(426):java。 io.FileNotFoundException:/ file:/ mnt/sdcard/OfflineBrowser/tit /hs29y05gei32/7mfm99n7it75.js(没有这样的文件或目录) 09-23 16:00:00.967:WARN/System.err(426):at org。 apache.harmony.luni.platform.OSFileSystem.open(Native Method) –