open a pdf file programmatically

后端 未结 7 2223
广开言路
广开言路 2021-01-06 15:45

I am working on pdf. I am trying to open a pdf file from my application using the code below. But I failed to open.

private void openPdf() {

        File fi         


        
7条回答
  •  半阙折子戏
    2021-01-06 15:48

    I have nearly identical code that works fine, though I'm not opening a file from SD card in my app.

    Activity mActivity = /* your activity */...;
    String mFileName = /* path of my PDF file */...;
    
    Uri uri  = Uri.fromFile(mActivity.getFileStreamPath(mFileName));
    
    try
    {
        Intent intentUrl = new Intent(Intent.ACTION_VIEW);
        intentUrl.setDataAndType(uri, "application/pdf");
        intentUrl.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        mActivity.startActivity(intentUrl);
    }
    catch (ActivityNotFoundException e)
    {
        Toast.makeText(mActivity, "No PDF Viewer Installed", Toast.LENGTH_LONG).show();
    }
    

    so your approach is right on. Make sure you can open the file first ... i.e. use mActivity.openFileInput() to ensure you have a readable PDF.

提交回复
热议问题