我的应用使用jsoup lib从网上下载数据。下载它的节目时没有找到这样的文件或目录。我使用CMD手动制作了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)
希望有人可以帮助我..
答案 0 :(得分: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();
}