Links to pdf files in HTML page using Android Studio Asset directory

Asked

Viewed 1,134 times

0

Hello. I have an HTML page on a server with several links to pdf files in windows and I’m bringing this same page and the code along with the pdfs for an App for Android. I’m using Android Studio and putting the files directly in the Assets directory, incorporating them into the App. Unfortunately the links now do not open the pdfs files. The html links have been modified to the new Asset directory as: href = "file://android_assest/name_do_arq.pdf". It’s like there is no PDF reader (Pdf view). When I open an index.html file by the App, in the same path as the Asset or open a pdf file by my "file explorer" embedded in the App the file opens normally. I just can’t open it by the html page link as shown earlier. I would appreciate if you had any tips to open this link since I would like to take advantage of the already built page. [! [insert image description here][1][1]

inserir a descrição da imagem aqui

The href s in blue are the calls within the html page. The test.html call opens normally but the other pdfs links do not.

When I try to open the PDF file by the link on the html page it does not open. It seems that the link is not interpreted although the.pdf test file is in the same directory as the test.html file. I am using the https://github.com/barteksc/AndroidPdfViewer linked in my app in my app on Android 8.0

The basic example html file to link to the pdf file is as follows: "the test.html file and the test.pdf file are in the same android App directory (android_asset directory).

href="test.pdf""tb does not open pdf"

href="http://www.google.com"> Google "opens google"

iframe src="test.pdf""iframe "tb does not open pdf"

I have tried using the link formats in href as p.example:

"href="android_asset/test.pdf"

"href="file://android_asset/test.pdf"

"href="file://android_asset/test.pdf"

in JS also: "embed src="test.pdf" type="application/pdf" witdh="100%" height="100%"

"Page not found or URL" error appears or else the background is white as if it was going to render the pdf and nothing happens.

Does anyone have any tips that can tell us why this might be happening? Thank you all

2 answers

0

I solved the problem by installing a web server for android and using the localhost address 127.0.0.1:8080 for my page. using Kotlin webview.loadUrl(href="127.0.0.1:8080/index.html") The pdfs files attached to my web page have been opened without problems. The pdfs files were placed with relative address. I used the same folder as index.html.

0

You can view or download the PDF, for example by opening it in your device’s built-in browser or in web viewing by embedding it into your application.

Open the pdf in the browser,

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(pdf_url));
startActivity(browserIntent);

Open in a webview in the app:

 Webview webView = (WebView) findViewById(R.id.webView1);
 webView.getSettings().setJavaScriptEnabled(true);
 webView.loadUrl(pdf_url);

Open in a PDF viewer (You must have some installed on your phone):

PackageManager packageManager = context.getPackageManager();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType("application/pdf");
packageManager.queryIntentActivities(intent,PackageManager.MATCH_DEFAULT_ONLY);

Intent pdfintent = new Intent();
pdfintent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(yourfile);
pdfintent.setDataAndType(uri, "application/pdf");
context.startActivity(pdfintent);

Manifest:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
  • I am using Kotlin. I have already tested the examples. Permissions are active. Access to pdf through html does not work. It seems that the lib pdf view I compiled in the application does not render the pdf files or the files are not found in the Asset directory. It seems that the files in the "Assets" directory are combined in the "release" and the external html call does not find the files.I will try: <tags> in Html5: <Object data="test.pdf#page=1" type="application/pdf" width="100%" height="100%"> <p><b>Examplo</b>: The browser does not support Pdfs. download to see it: <a href="test.pdf">Download</a>. </p> </Object>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.