1

I want to open File which is in InternalStorage

I created Documents directory under data/data/mypackage/files path .

Now I want to Open a file in some external application like PDF viewer or text viewer.

I already checked, the file exists but when I am opening file in specific viewer like PDF viewer,

it display Toast- "Can not display PDF".

I am using below code to open file in the specific viewer.

File file=new File(context.getFilesDir()+"/Documents/"+fileInfo.getFilename()+"."+fileInfo.getFiletype());
if (file.exists()){
    Uri uri = Uri.parse("file://"+file);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(uri, "application/pdf");
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    context.startActivity(intent);
}

Chooser will open for select PDF viewer.

How to Open PDF or Text File from internal Storage (data/data/com..somepackagename/files)?

Pooja Kamani
  • 51
  • 1
  • 7
  • try this intent.setDataAndType(uri, "*/*"); – Ratilal Chopda Jan 31 '18 at 11:10
  • 1
    Use `FileProvider`. As the one answer points out, other apps cannot access `getFilesDir()` of your app. Plus `Uri.fromFile()` will not work on Android 7.0+ devices, as `Uri` values built that way are banned from use in `Intents`. – CommonsWare Jan 31 '18 at 11:44

1 Answers1

1

Third party apps cant access your package folder, if you want to open that file third party apps , move that file into shared folder

user2851150
  • 397
  • 5
  • 12