2016-11-26 55 views
-2
File file = new File(s); 
    Intent intent = new Intent(Intent.ACTION_VIEW); 
    intent.setDataAndType(Uri.fromFile(file), "application/pdf"); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 
    try { 
     startActivity(intent); 
    } 
    catch(Exception e) 
    { 
     file.delete(); 
     downloadFile(file); 
    } 

我想在Android中使用外部应用程序打开PDF,如果文件已完全下载,它将打开并显示PDF文件。 如果文件为空或损坏,我想删除文件并重新下载。如何处理无法使用外部应用程序在Android中打开文件

,但我不能处理

例外“的文件无法打开”。

+0

你从哪里看到那个异常?显示堆栈跟踪。 – greenapps

+0

'(s)'。你认为's'的价值是无关紧要的? – greenapps

+0

你的pdf文件位置在哪里?因为它取决于第三方应用程序操作来查看。 –

回答

0

,但我不能够处理 例外“的文件无法打开”。

这很可能是因为它不在您显示的代码片段之内,其中downloadFile()是可能的代码。如果是这样的话,你需要在那里有try/catch(或者至少在downloadFile()方法调用)。在catch()不会让你自动捕捉任何后续的例外。

0

它可能发生您的PDF文件格式不正确,因此无法打开或者没有可用的应用程序支持打开PDF。

让我们来检查一下这个文件是否存在与否,如果文件存在,那么检查所有具有ACTION_VIEW意图过滤器的应用程序。

try { 
      File file = new File(s); 
      if (file.exists()) { 
       Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file)); 
       startActivity(intent); 
      } else { 
       downloadFile(file); 
      } 
     } catch (Exception e) { 
      file.delete(); 
      downloadFile(file); 
     } 
相关问题