我正在开发应用程序,需要在设备中打开pdf文件,
我实际上已经获得了与大多数示例类似的网络上的代码。但是,事情是我无法打开文件,控件直接转到“Exception”部分。
以下是以下代码:
public class MyPDFDemo extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button OpenPDF = (Button) findViewById(R.id.button);
OpenPDF.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
File pdfFile = new File("/sdcard/Determine_RGB_Codes_With_Powerpoint [PDF Library].pdf");
if(pdfFile.exists())
{
Uri path = Uri.fromFile(pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try
{
startActivity(pdfIntent);
}
catch(ActivityNotFoundException e)
{
Toast.makeText(MyPDFDemo.this, "No Application available to view pdf", Toast.LENGTH_LONG).show();
}
}
}
});
}
当我运行这个代码:我曾经看到“没有应用程序可以查看pdf”。任何人都可以请我查看pdf文件。