0

An Android app can create PDF files which should be stored to the external storage and viewed or shared by the user.

Creating, storing and sharing the file is no problem. However viewing the file fails and only a blank screen is shown.

This is my code:

<!-- Provider definition in manifest -->
<provider
    android:authorities="com.example.MyApp.fileprovider"
    android:name="android.support.v4.content.FileProvider"
    android:exported="false"
    android:grantUriPermissions="true" >
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>


<!-- file_path.xml in xml resource dir -->
<?xml version="1.0" encoding="utf-8"?>
<path xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="ExternalDir" path="Documents/MyApp" />
</path>


// Sharing and viewing
File baseDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
File appDir = new File(baseDir, "MyApp");
appDir.mkdirs();

File pdfFile = new File(appDir, "Test.pdf");
savePDFToFile(pdfFile)


Uri fileUri = FileProvider.getUriForFile(this, "com.example.MyApp.fileprovider", pdfFile);

// View
Intent viewIntent = new Intent(Intent.ACTION_VIEW);
viewIntent .setDataAndType(fileUri, "application/pdf");
viewIntent .addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
viewIntent .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(viewIntent , "View"));


// Sharing
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("application/pdf");
sharingIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
startActivity(Intent.createChooser(sharingIntent, "Share"));

Creating/Writing the PDF file is no problem. Thus there seems to be no problem with the permissions. Sharing the file (e.g. saving it to Google Drive or sending it via email) is also no problem. Thus the FileProvider config and Uri creation seems to be correct.

So, what is wrong with the viewing intent? When testing this in the Emulator using API 26 only a blank page is shown. A user running the same code on a device with API 26 gets a message, that file access is not possible (without a reason).

However the file exists and access permission shouldn't be problem because otherwise the sharing intent would not work as well, would it?

How to solve this?

Andrei Herford
  • 17,570
  • 19
  • 91
  • 225
  • if pdf view supported application is not installed in your phone, then it will not work – Hitesh Sarsava May 02 '18 at 10:08
  • The users phone has Adobe Acrobat installed. Thus this should not be a problem. Sharing with Google Drive in the emulator is no problem and as far as I know Googel Driver offers a build in PDF Viewer. So, should it be possible to view the file using Google Drive in the emulator? – Andrei Herford May 02 '18 at 10:18
  • google drive does not allow open external pdf file. its only open the saved pdf file in google drive. – Hitesh Sarsava May 02 '18 at 10:20
  • I have just tested it on another emulator running API 24 and installed Acrobat on it using the PlayStore. I can now choose between Google Drive and Acrobat to view the file. Acrobat shows a Toast message with the Text "This file could not be accessed. Check the location or the network and try again". Google Drive brings up the blank screen and switches back to my app... – Andrei Herford May 02 '18 at 10:35
  • do not test in emulator, test in real device so you will get the real error – Hitesh Sarsava May 02 '18 at 10:36
  • As explained the test on a real device with API 26 produces exactly the same error "This file could not be accessed..." Installing Acrobat in the emulator was only necessary to get the exact same message in emulator (instead of the blank screen when using Google Drive). So, the question is still the same: Why does sharing work but viewing does not? – Andrei Herford May 02 '18 at 10:44
  • its because the google drive made to share only file using deep linking. do you know about deep linking in android ? then search for it. it will gives you the full answer to your question. here is the links for example :https://developers.google.com/+/mobile/android/share/deep-link and https://stackoverflow.com/questions/35175055/android-share-deep-link-url-using-whatsapp-and-open-my-app-when-click-deep-link – Hitesh Sarsava May 02 '18 at 10:47
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/170209/discussion-between-andrei-herford-and-hitesh-sarsava). – Andrei Herford May 02 '18 at 10:48

0 Answers0