Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up
    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);
    }

I am trying to Open the PDF using External Application in Android, if the File is fully downloaded it will open and show the PDF File. If the File is empty or corrupted i want to delete the file and re Download it.

But i am not able to handle

the Exception "The Document can't be Opened".

share|improve this question
    
Where do you see that exception exactly? Show the stack trace. – greenapps 19 hours ago
    
(s). You think the value of s is irrelevanf? – greenapps 19 hours ago
    
Where is your pdf file location? Because it depends for 3rd party app action to view. – Ready Android 17 hours ago

But i am not able to handle the Exception "The Document can't be Opened".

It's most likely because it is either outside the code snippet you shown, with downloadFile() being the possible code. If it is so, you need to have try/catch there (or at least around downloadFile() method invocation). Being in catch() will NOT make you automagically catch any subsequent exceptions.

share|improve this answer

It can happen either your pdf file is not well formated so it is not able to open Or there is no application available which support to open pdf.

Let's check with this one for checking either file exists or not and if file exists then check for all applications which are having ACTION_VIEW intent filter.

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);
        }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.