在我的应用程序中,我试图从服务器下载 PDF 文件并将其存储在 SD 卡中,但是当我尝试下载时,下载总是失败并且 logcat 说找不到处理程序。我需要帮助来解决这个问题,请帮助我。谢谢你
下载Text.java
package mobile.download;
import java.io.*;
import java.net.*;
import java.util.regex.Pattern;
import mobile.config.Kondownload;
import com.karismaelearning.R;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.text.util.Linkify;
import android.util.Log;
import android.widget.TextView;
public class DownloadText extends Activity{
public Kondownload linkurl;
String url;
String SERVER_URL;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.linkdownload);
TextView mTextLink = (TextView) findViewById(R.id.LinkDownload);
Bundle bundle = this.getIntent().getExtras();
String param1 = bundle.getString("keyIdc");
String param2 = bundle.getString("keyReference");
linkurl = new Kondownload(this);
SERVER_URL = linkurl.getUrl();
SERVER_URL += "/moodledata/"+param1+"/"+param2;
URLConnection urlConnection = null;
try{
URL url = new URL(SERVER_URL);
//Opening connection of currrent url
urlConnection = url.openConnection();
urlConnection.connect();
//int lenghtOfFile = urlConnection.getContentLength();
String PATH = Environment.getExternalStorageDirectory() + "/pdf/";
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, param2);
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = url.openStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
System.out.println("--pdf downloaded--ok--"+SERVER_URL);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
mTextLink.setText(SERVER_URL);
Pattern pattern = Pattern.compile(SERVER_URL);
Linkify.addLinks(mTextLink, pattern, "");
}
}
日志猫
06-06 13:18:13.371: W/System.err(1056): java.io.FileNotFoundException: /mnt/sdcard/pdf/Dokumen/Dogmatika_1.pdf (No such file or directory)
06-06 13:18:13.371: W/System.err(1056): at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
06-06 13:18:13.391: W/System.err(1056): at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
06-06 13:18:13.391: W/System.err(1056): at java.io.FileOutputStream.<init>(FileOutputStream.java:94)
06-06 13:18:13.402: W/System.err(1056): at java.io.FileOutputStream.<init>(FileOutputStream.java:66)
06-06 13:18:13.402: W/System.err(1056): at mobile.download.DownloadText.onCreate(DownloadText.java:85)
06-06 13:18:13.402: W/System.err(1056): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-06 13:18:13.402: W/System.err(1056): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-06 13:18:13.402: W/System.err(1056): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-06 13:18:13.402: W/System.err(1056): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-06 13:18:13.402: W/System.err(1056): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-06 13:18:13.402: W/System.err(1056): at android.os.Handler.dispatchMessage(Handler.java:99)
06-06 13:18:13.402: W/System.err(1056): at android.os.Looper.loop(Looper.java:123)
06-06 13:18:13.402: W/System.err(1056): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-06 13:18:13.402: W/System.err(1056): at java.lang.reflect.Method.invokeNative(Native Method)
06-06 13:18:13.411: W/System.err(1056): at java.lang.reflect.Method.invoke(Method.java:507)
06-06 13:18:13.411: W/System.err(1056): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-06 13:18:13.411: W/System.err(1056): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-06 13:18:13.411: W/System.err(1056): at dalvik.system.NativeStart.main(Native Method)
06-06 13:18:13.861: I/ActivityManager(61): Displayed com.karismaelearning/mobile.download.DownloadText: +649ms
06-06 13:18:15.101: I/ActivityManager(61): Starting: Intent { act=android.intent.action.VIEW dat=http://10.0.2.2/moodledata/2/Dokumen/Dogmatika_1.pdf cmp=com.android.browser/.BrowserActivity (has extras) } from pid 1056
06-06 13:18:15.831: I/ActivityManager(61): Displayed com.android.browser/.BrowserActivity: +675ms
06-06 13:18:17.271: W/InputManagerService(61): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@40554818 (uid=10020 pid=378)
06-06 13:18:17.781: D/webviewglue(378): nativeDestroy view: 0x3e4820
06-06 13:18:18.541: I/DownloadManager(218): Initiating request for download 22
06-06 13:18:19.281: W/DownloadManager(218): Aborting request for download 22: no handler found for this download type