Android abriendo pdf no funciona

I've been trying to get this working for some time now. I've seen the other questions here about how to open pdfs from Android, and the general consensus is the code that I have below. Am I missing something?

        Uri path = Uri.parse("android.resource://com.TeamGLaDOS.DayTradeSim/" + R.raw.teamdoc);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(path, "application/pdf");
    catch(ActivityNotFoundException e)
        Toast.makeText(this, "No Application Available to view PDF",     Toast.LENGTH_SHORT).show();

Edit: It always throws the ActivityNotFoundException and shows the Toast.

The exception message is this:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=android.resource://com.TeamGLaDOS.DayTradeSim/2130968576 typ=application/pdf }

Editar 2: I have a pdf application installed (Aldiko) and I've used other apps to launch pdfs in Aldiko before.

preguntado el 08 de noviembre de 11 a las 19:11

What about it isn't working? Is there an error? If there's an error, what does your logcat say? -

@KurtisNusbaum I've updated with details. -

@KurtisNusbaum Thanks for deleting your answer and downvoting me just because I downvoted your answer! -

I didn't down vote you. I just removed my upvote. I deleted my answer because, as you pointed out, it was wrong. So I didn't want to leave an incorrect answer up. Should I have left an incorrect answer posted? -

2 Respuestas

Your URI is constructed appropriately to access a raw resource by ID, but that doesn't mean that all PDF readers will resolve an intent with an android.resource:// data URI scheme. It depends somewhat on the implementation of the PDF Reader application installed on the user's device, but many check more than just the mimeType set in your intent. Adobe Reader, for example, will resolve for any Intent with a file:// URI, content:// URI, or no URI at all (just the mimeType), but it will not resolve for a URI pointing directly at a resource.

To be as universal as possible, you should first copy any PDF file you want to read out of your assets/resources and onto the file system (internal or external storage, makes no difference as long as the file is created externally readable). Then pass the intent with a URI to the file instance, and you should have much better luck overall.

A quick test showed that Aldiko ONLY responds to an Intent with a file:// URI and "application/pdf" mimeType, and none of the other options that even Adobe Reader resolved there's your winner.


respondido 09 nov., 11:00

Thanks this seems to be what was causing the problem. I think we're going to go a different route though, to display the pdf, because copying it out of the package and onto the device is a little costly. (It's a large pdf). Thanks! - xdumaine

Try querying the packet manager:

PackageManager packageManager = getPackageManager();
Intent intent = new Intent(Intent.ACTION_VIEW);

List list = packageManager.queryIntentActivities(intent,PackageManager.MATCH_DEFAULT_ONLY);

if (list.size() > 0) {
    intent.setDataAndType(path, "application/pdf");

This will check to see if it finds anything to run pdfs with.

The URI looks fine but you can always catch FileNotFoundException which would be thrown if it can't find the pdf, and replace your package name with getPackageName() in case you're missing a character.

respondido 09 nov., 11:00

Interestingly, When I do this, list has size 1, but the exception is still thrown. - xdumaine

See my answer. That's because resolving an Intent with no data URI (mimeType only) and resolving an Intent with a URI + mimeType defined will result in a different set of available Activities :) - desconectado

You should be able to cast items in list as ResolveInfo to get the package name of the activity using list.get(0).resolvePackageName to rule out the possibility of it not finding Aldiko. - Dororo

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.