Links to pdf files on HTML page using Android Studio asset directory

0

Hello. I have an HTML page on a server with several links to pdf files on windows and I'm bringing this same page and the code along with the pdfs to an Android App. I'm using Android Studio and placing 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/arch_name.pdf". It is like there was no Pdf reader. When I open an index.html file through the App, in the same asset path or open a pdf file through my "file explorer" embedded in the App the file opens normally. I just can not open it by the html page link as shown above. I would appreciate if you had a tip to open this link since I would like to take advantage of the page already built. [!] [insert image description here] [1]] [1]

Thehref'sinbluearethecallsinsidethehtmlpage.Thetest.htmlcallnormallyopensbuttheotherpdfslinksdonot.

WhenItrytoopenthePDFfilethroughthelinkonthehtmlpageitdoesnotopen.Itseemsthatthelinkisnotinterpretedeventhoughthetest.pdffileisinthesamedirectoryasthetest.htmlfile.I'musing link in my app in my app on Android 8.0

The html file, basic example, to link to pdf file is as follows: "the test.html file and the test.pdf file are in the same directory as the android app (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've tried using the link formats in href as eg:

"href=" android_asset / test.pdf "

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

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

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

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

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

    
asked by anonymous 02.10.2018 / 21:39

2 answers

0

You can view or download the PDF, for example by opening it in the device's embedded browser or web view, incorporating 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 app:

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

Open in a PDF viewer (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>
    
03.10.2018 / 14:21
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 pdf files attached to my web page were opened without any problems. The pdf files were placed with relative address. I used the same folder as index.html.

    
30.10.2018 / 20:08