如何通过我的Android应用程序打开PDF文件?
我想通过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.
任何人都可以告诉我我要去哪里吗?
。
尝试了这一点:
Button btnOpen=(Button)findViewById(R.id.button1);
btnOpen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
File file = new File("/sdcard/sample.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) {
}
}
}
});
希望这会帮助你。
谢谢你的回复,当我把这个没有行动时,我点击按钮 ? – user1508544 2012-07-11 06:09:12
我已经在你的问题下发表评论看到它 – AkashG 2012-07-11 06:10:39
感谢你的回复我的pdf文件位置是/Root/sdcard/Download/sample.pdf – user1508544 2012-07-11 06:13:12
在这两个设备上安装了相同的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