How to read pdf from sdcard after download from server?

83 Views Asked by At

when download complete it is automatically crash..

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)

above error are detect on run time please give me solution of it.

1

There are 1 best solutions below

1
On

try this,

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();

}