0

I am facing hard time to preview PDF document present in SD card/internal memory in Android as it does not have its own API. I tried following:

Intent showIntent = new Intent(Intent.ACTION_VIEW);
showIntent.setDataAndType(uri, "application/pdf");
showIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(showIntent);

But its not working.

I tried checking using Webview as well but still no result.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web_view);

    String file = getIntent().getStringExtra("previewfile");

    webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl("http://docs.google.com/gview?embedded=true&url="+file);}

I have seen some libraries which can be used if the file is present in Asset folder but only problem is that I cant put file in asset folder programatically.

Is there any way of previewing the file stored in SD card? Please help.

Rahul Singh Chandrabhan
  • 2,531
  • 5
  • 22
  • 33
Munish
  • 186
  • 1
  • 2
  • 15

1 Answers1

0

Did you try all the answers to the question listed below?

How to open a PDF via Intent from SD card

The following code should be working fine.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
   File file=new File(mFilePath);
   Uri uri = FileProvider.getUriForFile(this, getPackageName() + ".provider", file);
   intent = new Intent(Intent.ACTION_VIEW);
   intent.setData(uri);
   intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
   startActivity(intent);
   } else {
   intent = new Intent(Intent.ACTION_VIEW);
   intent.setDataAndType(Uri.parse(mFilePath), "application/pdf");
   intent = Intent.createChooser(intent, "Open File");
   intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   startActivity(intent);
   }
Mithun M
  • 16
  • 2
  • Thanks Mithun. I have already done this and it worked. i have done the same as you mentioned :). – Munish May 25 '18 at 04:45
  • One more question, with this way the control goes out of activity. Means in case if need to fire another activity while previewing the document then I cant do right because the control goes to PDF viewer. i can only come back by clicking on back button but I cant go further? Any idea how that can be achieved.? – Munish May 25 '18 at 04:48