Intent.CreateChooser,它在android 8的最新更新后无效

时间:2018-04-13 11:35:34

标签: android api xamarin android-intent

我正在使用Xamarin.Android,因为我更新android使用API​​ 27 Intent.CreateChooser它不再工作.Below是我用于Chooser意图的代码序列它适用于api 25或更早但不适用于api 27 ,授予存储权限。谢谢你的帮助。

public class OpenFileServiceAndroid : IOpenFileService
{
    public async Task OpenFileService(string fileName)
    {
        string root = null;
        if (Android.OS.Environment.IsExternalStorageEmulated)
        {
            root = Android.OS.Environment.ExternalStorageDirectory.ToString();
        }
        else
        {
            root = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        }

        Java.IO.File file = new Java.IO.File(root + "/MyFolder", fileName);

        if (file.Exists())
        {
            Android.Net.Uri path = Android.Net.Uri.FromFile(file);
            string extension = 
                 Android.Webkit.MimeTypeMap.GetFileExtensionFromUrl(Android.Net.Uri.FromFile(file).ToString());

            string mimeType =
                 Android.Webkit.MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension);

            var intent = new Intent(Intent.ActionView, path);
            intent.SetDataAndType(path, mimeType);

            if (ContextCompat.CheckSelfPermission(Application.Context, Manifest.Permission.ReadExternalStorage) == Permission.Granted)
            {   
                Application.Context.StartActivity(Intent.CreateChooser(intent, "Choose App"));
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果targetSdkVersion高于24,则FileProvider用于授予访问权限。

请仔细按照以下步骤操作。[android.os.FileUriExposedException: file:///storage/emulated/0/test.txt][2] exposed beyond app through Intent.getData()此更新自nougat以来已完成..

希望这会对你有所帮助。