如何在我的 android 应用程序中阅读 pdf?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10299839/
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 read pdf in my android application?
提问by MAC
I am making an application which require to open pdf.
我正在制作一个需要打开pdf的应用程序。
I also have some pdf in asset folder so i am not able to open it in webview directly.
我在资产文件夹中也有一些 pdf,所以我无法直接在 webview 中打开它。
By default android does not support pdf.
默认情况下,android 不支持 pdf。
Is there any API that works on android(except MuPdf) ??
是否有任何适用于 android 的 API(MuPdf 除外)??
My device does not have any pdf reader installed so ACTION VIEW is not helpful for me
我的设备没有安装任何 pdf 阅读器,所以 ACTION VIEW 对我没有帮助
Following is not working.......
以下不起作用......
Render a PDF file using Java on Android
Open asset file pdf in application
can u suggest me any good api...
你能推荐我任何好的api吗?
thanks in advance...
提前致谢...
回答by Praveenkumar
I've simply done that using PdfViewer.jar(download it with the blue button) and making a code like below.
我只是使用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 Gautam Vasoya
Some phones (like the Nexus One) come with a version of Quickoffice pre-installed so it may be as easy as sending the appropriate Intent once you've saved the file to the SD card.
某些手机(例如 Nexus One)预装了 Quickoffice 版本,因此一旦您将文件保存到 SD 卡,就可以像发送适当的 Intent 一样简单。
public class OpenPdf extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.OpenPdfButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
File file = new File("/sdcard/example.pdf");
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(OpenPdf.this,
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
}
}
});
}
}