简体   繁体   English

Xamarin.Forms Android FileProvider:GrantWriteUriPermission并不总是有效

[英]Xamarin.Forms Android FileProvider: GrantWriteUriPermission not always working

I want to edit files from the internal storage of my Xamarin.Forms Android app in third party apps, for example fill out form elements in a PDF file or edit a .docx file. 我想在第三方应用程序中编辑来自我的Xamarin.Forms Android应用程序内部存储的文件,例如填写PDF文件中的表单元素或编辑.docx文件。

With my implementation the file gets correctly opened in the external app, but in certain apps it is opened read-only . 通过我的实现,文件在外部应用程序中正确打开,但在某些应用程序中,它以只读方式打开。 Adobe Acrobat and Microsoft Word open the files read-only, while other apps like Google Docs are able to write back into the file. Adobe Acrobat和Microsoft Word以只读方式打开文件,而其他应用程序(如Google Docs)则可以将文件写回文件。 (I am using Microsoft Word with a valid Office365 subscription). (我使用的是带有效Office365订阅的Microsoft Word)。

My FileProvider in the AndroidManifest.xml: 我在AndroidManifest.xml中的FileProvider:

<provider android:name="android.support.v4.content.FileProvider" 
          android:authorities="xamarintestapp.xamarintestapp.fileprovider" 
          android:grantUriPermissions="true" 
          android:exported="false">
    <meta-data android:name="android.support.FILE_PROVIDER_PATHS"     
               android:resource="@xml/filepaths" />
</provider>

filepaths.xml: filepaths.xml:

<paths>
        <files-path name="files" path="."/>
</paths>

Via the Xamarin.Forms DependencyService I am starting a Activity and pass the content uri to launch the external app: 通过Xamarin.Forms DependencyService我正在启动一个Activity并传​​递内容uri来启动外部应用程序:

public void OpenFile(string fileName)
{
    string auth = "xamarintestapp.xamarintestapp.fileprovider";
    string mimeType = Android.Webkit.MimeTypeMap.Singleton.GetMimeTypeFromExtension(Android.Webkit.MimeTypeMap.GetFileExtensionFromUrl(fileName.ToLower()));
    if (mimeType == null)
        mimeType = "*/*";

    var file = new Java.IO.File(Path.Combine(Forms.Context.FilesDir.Path, fileName));
    Android.Net.Uri uri = FileProvider.GetUriForFile(Forms.Context, auth, file);

    Intent intent = new Intent(Intent.ActionView);
    intent.SetDataAndType(uri, mimeType);
    intent.AddFlags(ActivityFlags.GrantReadUriPermission | ActivityFlags.GrantWriteUriPermission);
    intent.AddFlags(ActivityFlags.NewTask | ActivityFlags.NoHistory);

    // Trying to allow writing to the external app ...
    var resInfoList = Forms.Context.PackageManager.QueryIntentActivities(intent, PackageInfoFlags.MatchDefaultOnly);
    foreach (var resolveInfo in resInfoList)
    {
        var packageName = resolveInfo.ActivityInfo.PackageName;
        Forms.Context.GrantUriPermission(packageName, uri, ActivityFlags.GrantWriteUriPermission | ActivityFlags.GrantPrefixUriPermission | ActivityFlags.GrantReadUriPermission);
    }

    Forms.Context.StartActivity(intent);
}

Am I doing something wrong or is this simply not possible? 我做错了什么或者这根本不可能?

After further investigation I can confirm that this seems to be an issue of the app receiving my intent (eg Microsoft Word). 经过进一步调查后,我可以确认这似乎是接收我意图的应用程序的问题(例如Microsoft Word)。

When I start a EDIT intent instead of a VIEW intent as in 当我开始编辑EDIT而不是VIEW意图时

Intent intent = new Intent(Intent.ActionEdit);

the file is opened with write access in certain apps (xodo pdf, google docs) but read-only in other apps (Adobe PDF Reader, Microsoft Word) so I'm assuming these apps do not implement receiving an edit intent correctly. 该文件在某些​​应用程序(xodo pdf,google docs)中以写入权限打开,但在其他应用程序(Adobe PDF Reader,Microsoft Word)中只读,因此我假设这些应用程序未实现正确接收编辑意图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM