Is there a way to get the stock Android browser to auto-open a PDF, Word or other typical file without having to go through the process of downloading the file and then getting the user to open the file from the Downloads app or the Notification bar?
We have a web application that has a lot of documents that we'd like to include and not have开发者_Go百科 to convert to HTML, but making the user download the file and manually open it is not easy to train users on.
On iOS, these files all display inline in the browser. I'd like a way to get the browser to auto-launch the files into Acrobat Reader or QuickOffice or whatever program the user has to display them.
Does anyone know a way to do that? I know that Google Docs has some PDF viewing support, but people using our web app may not have public Internet access in all cases, and may be hitting on a local web server.
You can open a file PDF in Google Docs Viewer by appending the URL to:
https://docs.google.com/gview?embedded=true&url=<URL of a supported doc>
This would open a PDF file in the default browser or a WebView.
A list of supported formats is given here.
You can use this format as of 2017-04-06.
https://docs.google.com/viewerng/viewer?url=http://yourfile.pdf
Just replace http://yourfile.pdf with the link you use.
String format = "https://drive.google.com/viewerng/viewer?embedded=true&url=%s";
String fullPath = String.format(Locale.ENGLISH, format, "PDF_URL_HERE");
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(fullPath));
startActivity(browserIntent);
Specifically, to install the pdf.js plugin for Firefox, you do not use the app store. Instead, go to addons.mozilla.org from inside Mozilla and install it from there.
Also, to see if it's installed properly, go to the menu Tools → Add-ons (not the "about:plugins" URL as you might think from the desktop version).
I needed this too, and the links above stopped working so this is what I found to work with the New Google Drive:
Google has a service that creates the link for PDF's Not in GDrive: https://docs.google.com/viewer Just add your URL and it creates a link, and IFrame code (Look closely and you will see the pattern and create links without this web service)
Also, there is a way to do it for PDF's stored in Google Drive: https://docs.google.com/viewer?srcid=YOUR_GDRIVE_PDF_DOC_ID_HERE&pid=explorer&efh=false&a=v&chrome=false&embedded=true (this can be a link or the src URL of an iframe)
I've tested on Android and it brings up the PDF viewer nicely.
Unfortunately the native browser present on Android devices not support this type of file. Let's see if in the 4.0 we will be able to do that.
Try this code This worked for me.
package ak.wp.meto.activity;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.TextView;
import android.widget.Toast;
import com.github.barteksc.pdfviewer.PDFView;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import ak.wp.meto.R;
public class PdfViewer extends Activity {
private TextView txt; // You can remove if you don't want this
private PDFView pdf;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_pdf);
pdf = (PDFView) findViewById(R.id.pdfView); //github.barteksc
txt = findViewById(R.id.txtPdf);
String pdfUrl = "https://www.rkiinstruments.com/pdf/71-0136RK.pdf";
try{
new RetrievePdfStream().execute(pdfUrl);
}
catch (Exception e){
Toast.makeText(this, "Failed to load Url :" + e.toString(), Toast.LENGTH_SHORT).show();
}
}
class RetrievePdfStream extends AsyncTask<String, Void, InputStream> {
@Override
protected InputStream doInBackground(String... strings) {
InputStream inputStream = null;
try {
URL url = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
if (urlConnection.getResponseCode() == 200) {
inputStream = new BufferedInputStream(urlConnection.getInputStream());
}
} catch (IOException e) {
return null;
}
return inputStream;
}
@Override
protected void onPostExecute(InputStream inputStream) {
pdf.fromStream(inputStream).load();
}
}
}
add library in build.gradle
implementation 'com.github.barteksc:android-pdf-viewer:3.1.0-beta.1'
implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'
create activity_custom_pdf.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".activity.PdfViewer">
<com.github.barteksc.pdfviewer.PDFView
android:id="@+id/pdfView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:id="@+id/txtPdf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"/>
</RelativeLayout>
You can use this
webView.loadUrl("https://docs.google.com/viewer?url=" + "url of pdf file");
public class MainActivity extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openURL("http://docs.google.com/viewer?url=" + " your pdf link ");
}
});
}
private void openURL(String s) {
Uri uri = Uri.parse(s);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri,"text/html");
startActivity(intent);
}
}
Try the following. It worked for me.
WebView view = (WebView) findViewById(R.id.yourWebView);
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setPluginState(WebSettings.PluginState.ON);
view.loadUrl("http://docs.google.com/gview?embedded=true&url="
+"your document link(pdf,doc,docx...etc)");
Download the Acrobat Reader .apk file for Android to display PDF files
Put your PDF files on an SD card
Use the snippet of code below
File file = new File("/sdcard/test.pdf"); Uri path = Uri.fromFile(file); Intent i = new Intent(Intent.ACTION_VIEW); i.setDataAndType(path,"application/pdf"); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(i);
精彩评论