如何从服务器下载后从SD卡读取pdf?

时间:2014-05-21 11:01:40

标签: android pdfviewer

下载完成后会自动崩溃..

f (file.isFile()) {
        Intent mIntent = new Intent(SingleItemView.this,
                PdfViewerClass.class);
        mIntent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME,
                file.getAbsolutePath());
        startActivity(mIntent);
        finish();
    } else {
        downloadFile();
    }

05-21 16:26:03.138: E/WindowManager(14556): Activity com.example.pdfview.SingleItemView       has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{4197ab30 V.E..... R.....ID 0,0-465,326} that was originally added here
05-21 16:26:03.138: E/WindowManager(14556): android.view.WindowLeaked: Activity  com.example.pdfview.SingleItemView has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{4197ab30 V.E..... R.....ID 0,0-  465,326} that was originally added here
05-21 16:26:03.138: E/WindowManager(14556):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:409)
05-21 16:26:03.138: E/WindowManager(14556):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:218)
05-21 16:26:03.138: E/WindowManager(14556):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)

以上错误在运行时被检测到,请给我解决方案。

1 个答案:

答案 0 :(得分:1)

试试这个,

File pdfFile = new File(Environment

  .getExternalStorageDirectory(), "your.pdf");


try {

 if (pdfFile.exists()) {

  Uri path = Uri.fromFile(pdfFile);

  Intent objIntent = new Intent(Intent.ACTION_VIEW);

  objIntent.setDataAndType(path, "application/pdf");

  objIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

  startActivity(objIntent);

 } else {

  Toast.makeText(MainActivity.this, "File NotFound",

    Toast.LENGTH_SHORT).show();

 }

} catch (ActivityNotFoundException e) {

 Toast.makeText(MainActivity.this,

   "No Viewer Application Found", Toast.LENGTH_SHORT)

  .show();

} catch (Exception e) {

 e.printStackTrace();

}