4

I have a FileProvider in my app, and it pretends like it is working but the files it opens are blank. I have confirmed that the file is already on the device in the specified directory and is not blank, but when I try to open it from inside my app it fails. There is no crash and no error message. Has anyone seen this before?

FileProvider:

<provider android:name="android.support.v4.content.FileProvider" android:authorities="com.my.redacted.provider" android:exported="false" android:grantUriPermissions="true">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" />
        </provider>

provider_paths.xml

<?xml version="1.0" encoding="utf-8" ?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
  <external-path name="external_storage" path="." />
</paths>

The code:

    var path = "/storage/emulated/0/MyApp/User/attachments/folder/attachment.pdf"
    var file = new Java.IO.File(path);
    var mime = MimeTypeMap.Singleton;
    var ext = MimeTypeMap.GetFileExtensionFromUrl(path).ToLower();
    var type = mime.GetMimeTypeFromExtension(ext);

    var intent = new Intent();
    intent.SetAction(Intent.ActionView);
    var name = activity.PackageName + ".provider";
    var uri = FileProvider.GetUriForFile(activity.ApplicationContext, name, file);
    intent.SetDataAndType(uri, type);
    intent.SetFlags(ActivityFlags.GrantReadUriPermission);
    intent.SetFlags(ActivityFlags.GrantWriteUriPermission);

    var packageManager = activity.PackageManager;
    if (intent.ResolveActivity(packageManager) != null)
        activity.RunOnUiThread(() => activity.StartActivity(intent));
    else
    {
        var message = "No viewer for attachments: " + type;
        activity.RunOnUiThread(() =>
            Snackbar.Make(view, message, Snackbar.LengthLong).Show()
        );
    }

I have debugged to see what the URI ends up being, and it looks right:

uri.Path:

"/external_storage/MyApp/User/attachments/folder/attachment.pdf"

uri.ToString():

"content://com.my.redacted.provider/external_storage/MyApp/User/attachments/folder/attachment.pdf"

So I am really not sure what is going on.

NOTE: I DO have my documents in /storage/emulated/0/MY_STUFF and I did check to make sure that this is what <external-path> was returning.

Jason Toms
  • 850
  • 2
  • 10
  • 23
  • `new Java.IO.File(path);`. Show the value of path please. – greenapps Mar 02 '17 at 15:31
  • I added what the path is at that point to my question. The path does have the external directory as part of it. – Jason Toms Mar 02 '17 at 15:37
  • Also tell the values of uri.getPath() and uri.toString() please. – greenapps Mar 02 '17 at 15:45
  • `but when I try to open it from inside my app it fails`. Indeed you could use the uri to open the file within your app. But you are not doing that. Instead you use an intent to let the user choose a pdf reader app. Please clarify. – greenapps Mar 02 '17 at 15:50
  • I added the requested uri parameters. I am trying to open it FROM inside my app, not open the file inside my app. As in, I click on the file inside of my app and it launches the pdf viewer. Thank you for looking into this. – Jason Toms Mar 02 '17 at 15:52
  • You should call setFlags() only once as only the last one will have effect. OR the flags! – greenapps Mar 02 '17 at 15:52
  • So which app is it that cannot display the pdf? Blame that app! Try more pdf viewer apps. There are plenty. – greenapps Mar 02 '17 at 15:54
  • The app CAN display the pdf, it displays it just fine when I open it from the file explorer. – Jason Toms Mar 02 '17 at 15:56
  • Can be. Maybe the file explorer offers a file system path to it. But now you offer it a content scheme path. Are you shure the viewer can handle a content scheme? Try other apps. – greenapps Mar 02 '17 at 15:58
  • Welp. Feel free to post that as the answer. Apparently Google Drive can't handle content URIs. Which is kind of ridiculous. Downloaded Google PDF Viewer and it works fine. There goes 3 hours of my life... – Jason Toms Mar 02 '17 at 16:27
  • Use `intent.addFlags` instead of `intent.setFlags`. – 6rchid Apr 05 '20 at 05:40

2 Answers2

9

I had the same issue. No pdf reader could open my file provided from FileProvider.

My solution was adding the flag GRANT_READ_URI_PERMISSION:

intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_GRANT_READ_URI_PERMISSION);

Maybe your fault was overriding the FLAG_GRANT_READ_URI_PERMISSION in your code:

intent.setFlags(ActivityFlags.GrantReadUriPermission);
intent.setFlags(ActivityFlags.GrantWriteUriPermission); <- overrides the 1st flag
chrjs
  • 2,375
  • 1
  • 25
  • 41
0

The file explorer offers a file system path to it. But now you offer it a content scheme path. Are you shure the viewer can handle a content scheme? Try other apps.

greenapps
  • 11,154
  • 2
  • 16
  • 19