Search code examples
javaandroidandroid-intentexceptionandroid-4.2-jelly-bean

Android ActivityNotFoundException failing to catch on Intent in OS 4.1.2 (works on 4.0.3)?


Thanks for taking a look. I am a very old programmer but absolute newbie to Android/Java. Although I haven't found a way to make code colorful and pretty here yet ;).

I am starting an activity to open a file with a bogus/fake extension (mime type) so I can force this ActivityNotFoundException exception (to test).

Somehow, this exception never happens on my Samsung Galaxy S3 (OS 4.1.2). Adobe Reader opens up and errors that the file is not a PDF (it's not). However on an older ASUS Transformer tablet running 4.0.3 the exception is caught properly.

The code in question is like so:

// this file exists (check omitted).. 
File docFile = new File("/path/to/file.bogusextension");

// intent
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);

// getting doc Uri
Uri fileUri = Uri.fromFile(docFile);

// getting doc mimeType
MimeTypeMap mime = MimeTypeMap.getSingleton();
String extension = MimeTypeMap.getFileExtensionFromUrl(fileUrl);
String mimeType = mime.getMimeTypeFromExtension(extension);

intent.setDataAndType(fileUri, mimeType);

// this is for an adobe native extension, just getting FREContext
// in the class below and mapping functions
DocLauncherExtensionContext extensionContext = (DocLauncherExtensionContext) context;
Activity activity = extensionContext.getActivity();

try
{
    activity.startActivity(intent);                 
}
catch (ActivityNotFoundException e) 
{
    Toast.makeText(context.getActivity(), "This never happens!", Toast.LENGTH_LONG).show();                 
}

Is there anything blatent wrong with this code that would allow a file with any extension what-so-ever to be constantly thrown at Adobe Reader? I have cleared my defaults (Settings->Application Manager->Reset). Everything I startActivity is just hurled at Adobe Reader.

Any thoughts appreciated! Thank you!


Solution

  • Intent intent = new Intent();
    

    needs to become:

    Intent intent = new Intent(Intent.ACTION_VIEW);
    

    You were putting data and an ActionCode into the intent, however you never told it what it was an intent for.