Pdf reading stored in internal memory (the file path is not valid)

advertisements

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);


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.