mostafa-mansour1 / previewAnyFile

Cordova Plugin to preview any file in native mode by providing the local or external URL
MIT License
33 stars 31 forks source link

Pdf preview closed immediately on android #11

Closed jordan-jarolim closed 3 years ago

jordan-jarolim commented 3 years ago

Hi, I am trying to use the library to preview a file stored locally on the device (file is saved by Capacitor.Plugins.Filesystem). Everything works just ok on iOS, file preview is displayed as expected but I have troubles on android. File preview is closed immediately. File path is 'file:///storage/emulated/0/Documents/Payslip.pdf'. I also tried different locations.

I played a bit with the Java code and ended up having a solution which is proposed here: https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en. This is working for me, however, this is the exact opposite from what is mentioned in the readme about file:// vs content:// uri.

Specifically, I added this part in presentFile method:

private void presentFile(Intent intent, Uri uri, String type) {
    File myFile = new File(String.valueOf(uri).replace("file://", ""));
    Uri myUri = FileProvider.getUriForFile(this.context, this.context.getApplicationContext().getPackageName() + ".provider", myFile);
    intent.setDataAndType(myUri, type);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    this.cordova.getActivity().startActivityForResult(intent, 1);
  }

Could you please point me to a right direction what I might be doing wrong? I am absolutely new to android programming. You can find my working solution here: https://github.com/jordan-jarolim/previewAnyFile/commit/f783da335972f655629c334c0c90d5b2151edef1

Thank you.

jordan-jarolim commented 3 years ago

Now I am starting to understand your tiny hack there :) https://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed - would it make sense to completely migrate to content:// schema? I am happy to do a pull request in case you agree. But I am not sure about the other consequences.

Thank you

mostafa-mansour1 commented 3 years ago

if u can fix it, please do it and I will merge your changes

mostafa-mansour1 commented 3 years ago

I fix this with android FileProvider, kindly get the new version, and it has new features for preview the files from anywhere