【问题标题】:File extension when open custom file with Xamarin Android使用 Xamarin Android 打开自定义文件时的文件扩展名
【发布时间】:2017-09-12 01:12:14
【问题描述】:

我正在尝试在我的 Xamarin Android 应用程序中打开一个自定义文件,但是当我打开它时,我无法正确获取文件扩展名,甚至无法获取我的文件名。我尝试使用 OneDrive,它可以工作,但不能用于 Google Drive 甚至下载应用程序。

这是我的 AndroidManifest:

<application android:allowTaskReparenting="true" ...>
    <activity android:launchMode="singleTask" ...>          
        <intent-filter android:autoVerify="true">
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="content" android:mimeType="application/octet-stream" android:pathPattern=".*\\.pec" />
            <data android:scheme="content" android:mimeType="application/*" android:pathPattern=".*\\.pec" />
            <data android:scheme="content" android:mimeType="*/*" android:pathPattern=".*\\.pec" />
        </intent-filter>
        ...
    </activity>
</application>

我在 OnNewIntent 方法中获得了 Intent,因为我将 SingleTask 用作 LaunchMode。所以我的代码是这样的:

protected override void OnNewIntent(Intent intent)
{
   string action = intent.Action;
   string type = intent.Type; 
   AN.Uri fileUri = intent.Data; 
   string filename = fileUri.LastPathSegment; 
}

这就是我得到的 fileUri 和文件名。

  • 从 OneDrive: content://com.microsoft.skydrive.content.external/Drive/ID/1/Item/RID/8E63BB6D425F77DA%217395/Stream/1/Property/kn.pec

  • 来自 Google 云端硬盘: content://com.google.android.apps.docs.storage.legacy/enc%3DLU5kldI4Leii_fQEr6niunAH6hjV_WGJrQZDf64TSRRqnyqi

  • 来自下载: content://com.android.providers.downloads.documents/document/4169

【问题讨论】:

  • 您的意思是当您在Google Drive 或Downloads App 打开文件时,您无法获取文件扩展名和文件名?但是当您在One Drive 中打开文件时它确实有效?
  • @YorkShen-MSFT 没错!如果它甚至没有出现在 Uri 中,我如何从 Intent 中获取文件名?
  • 您是如何打开这些文件的?请发布您的代码。
  • 我无法重现您的问题,您能否分享一个可以重现此问题的基本演示?
  • @YorkShen-MSFT 这就是我打开文件的方式: AN.Uri fileUri = intent.Data;内存流数据 = 新的内存流();使用 (var input = this.ContentResolver.OpenInputStream(fileUri)) { input.CopyTo(data);输入。关闭();但真正的问题是我没有得到文件的真实文件名

标签: android-intent xamarin xamarin.android


【解决方案1】:

这就是我能够从下载和 android 上的谷歌驱动器中获取文件名的方式:

internal static bool TryGetFileNameFromUri(Android.Net.Uri uri, out string displayName)
{
    var result = false;
    displayName = default;

    var resolver = MainActivity.Instance.ContentResolver;

    if (uri.ToString().StartsWith("content://"))
    {
        Android.Database.ICursor cursor = null;
        try
        {
            cursor = resolver.Query(uri, null, null, null, null);
            if (cursor != null && cursor.MoveToFirst())
            {
                displayName = cursor.GetString(cursor.GetColumnIndex(OpenableColumns.DisplayName));
                result = true;
            }
        }
        finally
        {
            cursor.Close();
        }
    }

    return result;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多