您好我正在制作Android应用程序,从某些给定的URL读取pdf,然后将其存储在SD卡中,然后使用我的Android设备中的mupdf阅读器打开它。
我在logcat中收到以下错误:
logcat:
02-15 05:47:10.276: E/AndroidRuntime(3134): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ohile.openpdf/com.ohile.openpdf.MainActivity}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/pdf/Read.pdf typ=application/pdf }
02-15 05:47:10.276: E/AndroidRuntime(3134): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-15 05:47:10.276: E/AndroidRuntime(3134): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-15 05:47:10.276: E/AndroidRuntime(3134): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-15 05:47:10.276: E/AndroidRuntime(3134): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-15 05:47:10.276: E/AndroidRuntime(3134): at android.os.Handler.dispatchMessage(Handler.java:99)
02-15 05:47:10.276: E/AndroidRuntime(3134): at android.os.Looper.loop(Looper.java:137)
02-15 05:47:10.276: E/AndroidRuntime(3134): at android.app.ActivityThread.main(ActivityThread.java:5039)
02-15 05:47:10.276: E/AndroidRuntime(3134): at java.lang.reflect.Method.invokeNative(Native Method)
02-15 05:47:10.276: E/AndroidRuntime(3134): at java.lang.reflect.Method.invoke(Method.java:511)
02-15 05:47:10.276: E/AndroidRuntime(3134): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-15 05:47:10.276: E/AndroidRuntime(3134): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-15 05:47:10.276: E/AndroidRuntime(3134): at dalvik.system.NativeStart.main(Native Method)
02-15 05:47:10.276: E/AndroidRuntime(3134): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/pdf/Read.pdf typ=application/pdf }
02-15 05:47:10.276: E/AndroidRuntime(3134): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1622)
02-15 05:47:10.276: E/AndroidRuntime(3134): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
02-15 05:47:10.276: E/AndroidRuntime(3134): at android.app.Activity.startActivityForResult(Activity.java:3370)
02-15 05:47:10.276: E/AndroidRuntime(3134): at android.app.Activity.startActivityForResult(Activity.java:3331)
02-15 05:47:10.276: E/AndroidRuntime(3134): at android.app.Activity.startActivity(Activity.java:3566)
02-15 05:47:10.276: E/AndroidRuntime(3134): at android.app.Activity.startActivity(Activity.java:3534)
02-15 05:47:10.276: E/AndroidRuntime(3134): at com.ohile.openpdf.MainActivity.showPdf(MainActivity.java:46)
02-15 05:47:10.276: E/AndroidRuntime(3134): at com.ohile.openpdf.MainActivity.onCreate(MainActivity.java:32)
02-15 05:47:10.276: E/AndroidRuntime(3134): at android.app.Activity.performCreate(Activity.java:5104)
02-15 05:47:10.276: E/AndroidRuntime(3134): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-15 05:47:10.276: E/AndroidRuntime(3134): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-15 05:47:10.276: E/AndroidRuntime(3134): ... 11 more
我写了以下java代码:
* DownloadFile.java *
public class DownloadFile {
public static void downloadFile(String fileUrl, File directory) {
try {
FileOutputStream f = new FileOutputStream(directory);
URL u = new URL(fileUrl);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
InputStream in = c.getInputStream();
byte []buffer = new byte[1024];
int len = 0;
while((len = in.read(buffer))>0) {
f.write(buffer,0,len);
}
f.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
MainActivity.java
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String extStorageDirectory = Environment.getExternalStorageDirectory()
.toString();
File folder = new File(extStorageDirectory, "pdf");
folder.mkdir();
File file = new File(folder, "Read.pdf");
try {
file.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}
DownloadFile.downloadFile("http://14.140.41.194/monali/i.pdf",
file);
showPdf();
}
public void showPdf()
{
File file = new File(Environment.getExternalStorageDirectory()+"/pdf/Read.pdf");
PackageManager packageManager = getPackageManager();
Intent testIntent = new Intent(Intent.ACTION_VIEW);
testIntent.setType("application/pdf");
List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/pdf");
startActivity(intent);
}
}
有人可以帮我解决代码中的错误吗? ???
答案 0 :(得分:0)
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.parse("file://" + getFilesDir() + "/example.pdf"),
"application/pdf");
startActivity(intent);
}