在Android中打开PDF文件
我试图打开一个pdf文件的链接,并在设备上单击按钮时显示pdf。最好的办法是什么?我希望能够不使用第三方软件。因此,我知道可能需要将文件转换为其他文件。在按钮的单击侦听器中尝试以下操作:在Android中打开PDF文件,android,pdf,file-io,viewer,Android,Pdf,File Io,Viewer,我试图打开一个pdf文件的链接,并在设备上单击按钮时显示pdf。最好的办法是什么?我希望能够不使用第三方软件。因此,我知道可能需要将文件转换为其他文件。在按钮的单击侦听器中尝试以下操作: File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/example.pdf"); Intent intent = new Intent(Intent.ACTION_VIEW); intent.set
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/example.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),”application/pdf”);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
在使用此代码之前,您应该检查是否有任何PDF阅读应用程序可用
如果您计划实现自己的PDF阅读器,请参考我认为您应该创建自己的PDF阅读器,您必须下载PDF VIEWER.jar 然后试试这个代码 MainActivity.java
public class MainActivity extends ListActivity {
String[] pdflist;
File[] imagelist;
@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));
}
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(MainActivity.this, Second.class);
intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, path);
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
}
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.ic_launcher;
}
public int getNextPageImageResource() {
return R.drawable.ic_launcher;
}
public int getZoomInImageResource() {
return R.drawable.ic_launcher;
}
public int getZoomOutImageResource() {
return R.drawable.ic_launcher;
}
public int getPdfPasswordLayoutResource() {
return R.drawable.ic_launcher;
}
public int getPdfPageNumberResource() {
return R.drawable.ic_launcher;
}
public int getPdfPasswordEditField() {
return R.drawable.ic_launcher;
}
public int getPdfPasswordOkButton() {
return R.drawable.ic_launcher;
}
public int getPdfPasswordExitButton() {
return R.drawable.ic_launcher;
}
public int getPdfPageNumberEditField() {
return R.drawable.ic_launcher;
}
}
看看[这个][1]静默,希望这能有所帮助。[1] :你可以试试这个..最简单的方法是使用Google docs viewer加载pdf。这样就可以在web视图中完成。但缺点是功能不多。