6

I wanna open a pdf stored in Mobile internal Memory... i'm trying to pass the path of the file to the Uri ..but it keep showing me this error "The file path is not Valid" but i'm sure that i'm putting the right path..

    targetFile=new File("/data/data/package Name/app_mydir/test.pdf");

            }

        Intent intent;
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(targetFile), "application/pdf");


               startActivity(intent);
Reham
  • 1,916
  • 6
  • 21
  • 45
  • How is your file getting saved? Generally data files end up inside a folder named "files" in your app dir, not at the root. – FoamyGuy Dec 16 '11 at 19:54
  • i'm using this directory = getBaseContext().getDir("mydir", Context.MODE_PRIVATE); } File file1 = new File(directory, PdfName); – Reham Dec 16 '11 at 20:02
  • replace "getBaseContext()" with "YourActivityName.this" Also you are probably not going to be able to use MODE_PRIVATE if you are trying to pass the file along to another application. – FoamyGuy Dec 16 '11 at 20:06
  • i'm using the same code to save and read Audio Files ,,and it work... but it does not work with pdf files – Reham Dec 16 '11 at 20:11
  • What PDF viewer do you have installed? I don't think Adobe reader supports launching view intents like this, you'd have to explicitly choose that as the app you wish to use to open if you are going with that one. There are some 3rd party ones that work with the way you have it now though. – FoamyGuy Dec 16 '11 at 20:55

2 Answers2

7

Files in the internal storage directory of your app are by default private to your application. Which means that no PDF-Reader app can read that file (since it doesn't run with your apps pid - no read permission is given).

You have to save that PDF with explicit reading permissions for other apps by using the Context.MODE_WORLD_READABLE flag. See the data storage documentation how to do that exactly.

Also use Context.openFileOutput() and Context.openFileInput() to read and write files in your internal directory (as mentioned in the docs above). Don't hardcode paths like this, they might change.

  • the question is about internal storage which is public to all apps ,only cache dir and data dir are private to an app. ( assume as a phone has a internal storage of 32 GB and external sd card of 32 GB , here question is referring to that internal storage) – Ravikant Tiwari Jul 16 '18 at 11:56
1

Check the output of

targetFile.exists();

If this returns true then you are good to go otherwise something is wrong with your path.

Udo Held
  • 12,314
  • 11
  • 67
  • 93