我想通過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