【问题标题】:xamarin form android need to open pdf with my app (no show)xamarin form android需要用我的应用程序打开pdf(没有显示)
【发布时间】:2018-05-21 20:22:43
【问题描述】:

我正在使用这个guide 在我的应用程序中打开一个 pdf。我只想在按“共享”时在可以打开文件(pdf)的应用程序列表中看到我的应用程序。 所以,到目前为止,我的 iOS 项目可以正常工作,所以这是一个 Android 设置问题。

主活动:

[Activity(Label = "Test", Icon = "@drawable/icon", Theme = "@style/MyTheme", MainLauncher = true)]
[IntentFilter(
    new[] { Intent.ActionView },
   Categories = new[]
  {
      Intent.CategoryDefault,
      Intent.CategoryBrowsable,
  },
  DataScheme = "file",
  DataMimeType = "*/*",
  DataPathPattern = ".*\\.pdf"
)]

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(bundle);

        string action = Intent.Action;
        string type = Intent.Type;

        global::Xamarin.Forms.Forms.Init(this, bundle);

        var application = new App();

        if (Intent.ActionView.Equals(action) && !String.IsNullOrEmpty(type))
        {
            Android.Net.Uri fileUri = Intent.Data;

            if (fileUri != null)
            {
                var fileContent = File.ReadAllBytes(fileUri.Path);
                var name = fileUri.LastPathSegment;
                application.SetExternDocument(new IncomingFile()
                {
                    Name = name,
                    Content = fileContent
                });
            }
        }

        LoadApplication(application);
    }
}

和 Manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
android:versionCode="1" 
android:versionName="1.0" 
package="com.bbinternational.inviofatture.BBInvioFatture">

<uses-sdk android:minSdkVersion="21" />

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />

如您所见,我没有在清单中写入任何内容,因为我在主 Activity 中使用了属性。 我尝试使用清单,但我无法使用 Xamarin 检索 MainActivity android:name 属性(在运行时它分配一些随机值,或者至少是我所知道的)。

感谢您的帮助。

大家好。

【问题讨论】:

  • 试试这个代码Device.Openuri(new uri(file));。希望对您有所帮助或查看此链接:https://forums.xamarin.com/discussion/25746/how-to-open-pdf-in-xamarin-forms

标签: android pdf xamarin xamarin.forms


【解决方案1】:

所以,经过几个小时的谷歌搜索,这个家伙救了我。

https://codemilltech.com/sending-files-to-a-xamarin-forms-app-part-2-android/

我认为 IntentFilterAttributes 是错误的。这是正确的版本

[IntentFilter(new[] { Intent.ActionSend }, Categories = new[] { Intent.CategoryDefault }, DataMimeType = @"application/pdf")]

希望能帮助到别人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-26
    • 1970-01-01
    • 1970-01-01
    • 2017-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多