我想通过Android应用程序打开PDF文件。到目前为止,我已经写了下面的代码:如何通过我的Android应用程序打开PDF文件?
public class PdfopenActivity extends Activity {
String selectedFilePath;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnOpen=(Button)findViewById(R.id.button1);
btnOpen.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Sample.pdf is my file name it is in /Root/Download/Sample.pdf
// path of the file
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Sample.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 175kb
,我可以直接打开该文件在我的银河TAB2平板电脑,但是当我跑我的我的程序来打开它,我得到的错误:
An Error Occurred while opening the document.
任何人都可以告诉我我要去哪里吗?
。
在这两个设备上安装了相同的PDF查看器? – user370305 2012-07-11 05:57:19
请确保PDF位于sdcard.if中的正确位置,如果它位于任何文件夹内部,那么它将包含在内。 – AkashG 2012-07-11 06:05:59
通过编写Environment.getExternalStorageDirectory()。getAbsolutePath()+“/ Sample.pdf”,它将显示它是不是在任何文件夹,但在SD卡内 – AkashG 2012-07-11 06:06:56