Xamarin.Forms Android FileProvider:GrantWriteUriPermission并非始终有效 [英] Xamarin.Forms Android FileProvider: GrantWriteUriPermission not always working

查看:213
本文介绍了Xamarin.Forms Android FileProvider:GrantWriteUriPermission并非始终有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从第三方应用程序中的Xamarin.Forms Android应用程序的内部存储中编辑文件,例如在PDF文件中填写表单元素或编辑.docx文件.

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.

在我的实施中,该文件已在外部应用程序中正确打开,但在某些应用程序中已打开只读. Adobe Acrobat和Microsoft Word以只读方式打开文件,而其他应用程序(例如Google Docs)则可以写回到文件中. (我将Microsoft Word与有效的Office365订阅一起使用).

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. (I am using Microsoft Word with a valid Office365 subscription).

AndroidManifest.xml中的我的FileProvider:

My FileProvider in the AndroidManifest.xml:

<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>

通过Xamarin.Forms DependencyService,我正在启动一个Activity,并传递内容uri以启动外部应用程序:

Via the Xamarin.Forms DependencyService I am starting a Activity and pass the content uri to launch the external app:

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?

推荐答案

经过进一步调查,我可以确认这似乎是应用程序收到我的意图的问题(例如Microsoft Word).

After further investigation I can confirm that this seems to be an issue of the app receiving my intent (e.g. Microsoft Word).

当我启动EDIT意图而不是VIEW意图时

When I start a EDIT intent instead of a VIEW intent as in

Intent intent = new Intent(Intent.ActionEdit);

该文件在某些​​应用程序(xodo pdf,google docs)中以写访问权限打开,但在其他应用程序(Adobe PDF Reader,Microsoft Word)中为只读,因此我假设这些应用程序未实现正确接收编辑意图

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.

这篇关于Xamarin.Forms Android FileProvider:GrantWriteUriPermission并非始终有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆