Limitations on Opening Pdf File in Android

Limitations on opening pdf file in Android

I found a workaround to view my PDF on my Android application (but does not allow me to download it after show on the application). If I open my PDF using Google Docs I can open it with my Intent.

This is what I had to add before my url:

https://docs.google.com/gview?embedded=true&url=

so the final url will be like:

https://docs.google.com/gview?embedded=true&url=https://myUrl.pdf

Here is the whole code I am using right now:

String url= "https://docs.google.com/gview?embedded=true&url=https://url.pdf";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);

But it is still not enough, because I would like to open it without need of using a third party application. Also, opening PDF with Google Docs is slow and I have to wait too much until the PDF is finally opened.

If anyone knows how to open it with native Android please let me know.


What happens if I do not open it with Google Docs?

With the same code, but just using my url, instead the added Google Docs url, Android let me choose between two applications: Adobe Acrobat Reader and Google Chrome.

  • If I open it with Google Chrome it download the PDF but it is not opened automatically.

  • If I open it with Adobe Acrobat Reader, it gives to me the following error:

    The PDF cannot be shown (it cannot be opened)

Problems displaying pdf files on pdf apps using Android 10

Problem solved. This code should be placed on Manifest according to this link:

<!-- This attribute is "false" by default on apps targeting
Android 10 or higher. -->
<application android:requestLegacyExternalStorage="true" ... >
...
</application>

Cannot open PDF file in external app

Ok guys, problem solved!

This is the code that i use to open a PDF file, stored on external memory:

File pdfFile = new File(Environment.getExternalStorageDirectory(),"namePdfFile.pdf");//File path
if (pdfFile.exists()) //Checking if the file exists or not
{
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);//Starting the pdf viewer
} else {

Toast.makeText(getActivity(), "The file not exists! ", Toast.LENGTH_SHORT).show();

}

How to prevent complete action using while opening pdf file using Adobe Reader.

I would like to thanks every one who tried to answer this question. After some browsing i found solution for my Question. which is perfectly working for me.

instead of using

intent.setPackage("com.adobe.reader");

I used

intent.setClassName("com.adobe.reader", "com.adobe.reader.AdobeReader");

don't forget to start activity in try catch block, it helps when adobe reader not installed on Device. Please check the below snippet.

try {
Intent intent = new Intent();

intent.setClassName("com.adobe.reader", "com.adobe.reader.AdobeReader");
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(doc), "application/pdf");
startActivity(intent);

}
catch (ActivityNotFoundException activityNotFoundException) {
activityNotFoundException.printStackTrace();
}

Opening PDF file using Android intent

Please set intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

It will grant access of your file to other providers

Open PDF file in my android application without using any browser or third party application

Send file name from FirstActivity to SecondActivity.

Intent i=new Intent(FirstActivity.this, SecondActivity.class);
i.putExtra("file", fileUrl);
startActivity(i);

in SeconActivity, Use WebView

EDIT

Intent i = getIntent();
String myPdfUrl = i.getStringExtra("file");
String url = "http://docs.google.com/gview?embedded=true&url=" + myPdfUrl;
String doc="<iframe src='"+url+"' width='100%' height='100%' style='border: none;'></iframe>";
WebView web=(WebView)findViewById(R.id.webViewpdf);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadData(doc, "text/html", "UTF-8");

layout.xml

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webViewpdf"/>

</LinearLayout>


Related Topics



Leave a reply



Submit