如何通过Android应用程序打开PDF文件?
我想通过Android应用程序打开pdf文件。到目前为止,我已经编写了以下代码:如何通过Android应用程序打开PDF文件?,android,android-intent,path,launching-application,Android,Android Intent,Path,Launching Application,我想通过Android应用程序打开pdf文件。到目前为止,我已经编写了以下代码: public class PdfopenActivity extends Activity { String selectedFilePath; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentV
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
,我可以直接在Galaxy Tab2平板电脑上打开该文件,但当我运行我的程序打开该文件时,会出现以下错误:
打开文档时出错
谁能告诉我哪里出了问题吗?.试试这个:
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) {
}
}
}
});
希望这能对您有所帮助。尝试一下:
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) {
}
}
}
});
希望这能对您有所帮助。在这两种设备上安装的PDF查看器相同?请确保PDF位于SD卡中的正确位置。如果它位于任何文件夹中,也可以通过写入环境将其包括在内。getExternalStorageDirectory().getAbsolutePath()+“/Sample.PDF”它显示它不在任何文件夹中,而是在sdcard/Root/sdcard/Download/sample.pdfy中。您必须将下载作为/sdcard/Download/sample.pdfy包含在两个设备中安装的相同PDF查看器中?确保PDF位于sdcard中的正确位置。如果它位于任何文件夹中,则还应通过写入来包含它Environment.getExternalStorageDirectory().getAbsolutePath()+“/Sample.pdf”它表明它不在任何文件夹中,而是在sdcard/Root/sdcard/Download/sample.pdfy中。您必须将下载作为/sdcard/Download/sample.pdfthanks包含在您的回复中。当我点击按钮时,没有执行任何操作?我已经在您的问题下发表了评论。请参阅Ithankd获取您的回复我的pdf文件位置是/Root/sdcard/Download/sample.pdf感谢您的回复当我点击按钮时没有执行任何操作?我已经在您的问题下发表了评论请参见itthankd感谢您的回复我的pdf文件位置是/Root/sdcard/Download/sample.pdf