如何在 Android 中打开 PDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10480974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to open PDF in Android
提问by Sudhanshu Srivastava
How to open pdf file from server without saving it on device and without using any third party application.because i don't want my user to download any application to use my apps.And i don't want to use web view for opening pdf file.
如何在不将其保存在设备上且不使用任何第三方应用程序的情况下从服务器打开 pdf 文件。因为我不希望我的用户下载任何应用程序来使用我的应用程序。而且我不想使用 Web 视图来打开 pdf文件。
回答by Pallavi
This method worked in the older versions of android:
此方法适用于旧版本的 android:
In a new activity:
在新活动中:
WebView webview = new WebView(this);
setContentView(webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setPluginsEnabled(true);
webview.loadUrl("http://docs.google.com/gview?embedded=true&url=URL_of_PDF/fileName.pdf");
setContentView(webview);
give internet
permission in manifest file.
internet
在清单文件中授予权限。
USE the following method to open a PDF file with already installed 3rd party application.
使用以下方法打开已安装第 3 方应用程序的 PDF 文件。
To open the PDF in application use:
要在应用程序中打开 PDF,请使用:
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
回答by Shankar Agarwal
use PdfViewer.jarand making a code like below - copied from here
使用PdfViewer.jar并制作如下代码 - 从此处复制
First.java
首先.java
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
File images = Environment.getExternalStorageDirectory();
imagelist = images.listFiles(new FilenameFilter()
{
public boolean accept(File dir, String name)
{
return ((name.endsWith(".pdf")));
}
});
pdflist = new String[imagelist.length];
for(int i = 0;i<imagelist.length;i++)
{
pdflist[i] = imagelist[i].getName();
}
this.setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, pdflist));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
String path = imagelist[(int)id].getAbsolutePath();
openPdfIntent(path);
}
private void openPdfIntent(String path)
{
try
{
final Intent intent = new Intent(First.this, Second.class);
intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, path);
startActivity(intent);
}
catch (Exception e)
{
e.printStackTrace();
}
}
Second.java
第二个.java
public class Second extends PdfViewerActivity
{
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
public int getPreviousPageImageResource() {
return R.drawable.left_arrow;
}
public int getNextPageImageResource() {
return R.drawable.right_arrow;
}
public int getZoomInImageResource() {
return R.drawable.zoom_in;
}
public int getZoomOutImageResource() {
return R.drawable.zoom_out;
}
public int getPdfPasswordLayoutResource() {
return R.layout.pdf_file_password;
}
public int getPdfPageNumberResource() {
return R.layout.dialog_pagenumber;
}
public int getPdfPasswordEditField() {
return R.id.etPassword;
}
public int getPdfPasswordOkButton() {
return R.id.btOK;
}
public int getPdfPasswordExitButton() {
return R.id.btExit;
}
public int getPdfPageNumberEditField() {
return R.id.pagenum_edit;
}
}
Hope this helps you lot. Try this. Don't forgot to add your Second.java in your manifest. Add some drawables whatever it requires in second.java with your drawables. And, Refer the example from GitHub
希望这对你有很大帮助。尝试这个。不要忘记在清单中添加您的 Second.java。在 second.java 中使用您的可绘制对象添加一些它需要的可绘制对象。并且,请参考GitHub 中的示例
回答by Hymanson Chengalai
for this u have to use pdf reader libraries.this link will help u
为此,您必须使用 pdf 阅读器库。此链接将帮助您
https://stackoverflow.com/questions/4665957/pdf-parsing-library-for-android
https://stackoverflow.com/questions/4665957/pdf-parsing-library-for-android