Emanuel0101
Emanuel0101

Reputation: 113

Open PDF-File in Android not working

I try to open a pdf file from my apps directory through a pdf viewer on the device.

PackageManager m = getPackageManager();
String s = getPackageName();
                PackageInfo p;
                try {
                    p = m.getPackageInfo(s, 0);
                     s = p.applicationInfo.dataDir;
                } catch (NameNotFoundException e) {                     
                    Log.w("Error", "Error Package not found ", e);                     
                }

                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(s + "\\Document.pdf"));
intent.setType("application/pdf");                  
PackageManager pm = getPackageManager();                    
Intent crC = Intent.createChooser(intent, "Open File"); startActivity(crC);

On the test device is an pdf viewer installed. Nevertheless I'm told that no existing app is able to open that file. Am I doing something wrong?

Upvotes: 0

Views: 1386

Answers (2)

Emanuel0101
Emanuel0101

Reputation: 113

If anybody is still interested, here is my solution:

I simply changed the code from this:

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(s + "\\Document.pdf"));
intent.setType("application/pdf"); 

to this:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(s + "\\Document.pdf"), "application/pdf"); 

And suddenly it worked. :)

Upvotes: 1

A J
A J

Reputation: 1120

What is the following code supposed to do?

PackageManager pm = getPackageManager();                    
Intent crC = Intent.createChooser(intent, "Open File"); startActivity(crC);

I assume you looked at a few tutorials and got a mixture of everything in here. Instead of those two lines, just try

startActivity(intent);

Does that not work?

Upvotes: 0

Related Questions