zoukankan      html  css  js  c++  java
  • Andrioid FileProvider在Xamarin.Forms中的使用

    Andrioid FileProvider在Xamarin.Forms中的使用

    Android 7.0到来后,为了进一步提高私有文件的安全性,Android不再由开发者放宽私有文件的访问权限,之前我们一直使用"file:///"绝对路径来传递文件地址的方式,

    在接收方访问时会触发SecurityException的异常。

    因此在提供文件给第三方应用访问时,我们就会用到FileProvider。

    FileProvider使用方法:

    1.在AndroidManifest.xml里声明Provider

     <application android:label="AppTest.Android">
        <provider android:name="android.support.v4.content.FileProvider" 
        android:authorities="应用包名.fileprovider"
        android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> </application>

     注意:其中android:authorities="应用包名.fileprovider" 如下使用最好android:authorities="${applicationId}.fileprovider",这样可以跟随包名变化。

    2. 配置FileProvider文件共享的路径

    <?xml version="1.0" encoding="utf-8"?>
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
        <root-path name="root" path="" />
        <files-path name="files" path="" />
        <cache-path name="cache" path="" />
        <external-path name="external" path="" />
        <external-files-path name="name" path="path" />
         <external-cache-path name="name" path="path" />
    </paths>

    说明:

    在paths节点内部支持以下几个子节点,分别为:

    • <root-path/>  代表设备的根目录new File("/");
    • <files-path/>  代表context.getFilesDir()
    • <cache-path/>  代表context.getCacheDir()
    • <external-path/>  代表Environment.getExternalStorageDirectory()
    • <external-files-path> 代表context.getExternalFilesDirs()
    • <external-cache-path> 代表getExternalCacheDirs()

    3. 配置完共享地址后,获取content uri的值,这个uri即提供给第三方进行访问的uri地址

    例子为打开文件代码:

            public void OpenFile(string path)
            {
                var context = Android.App.Application.Context;
                try
                {
                    if (File.Exists(path))
                    {
                        var file = new Java.IO.File(path);
                        var provider = context.PackageName + ".fileprovider";
                        
                        var uri = FileProvider.GetUriForFile(context, provider, file);
                        Intent intent = new Intent(Intent.ActionView);
                        intent.SetData(uri);
                        intent.AddFlags(ActivityFlags.GrantReadUriPermission);
                        intent.AddFlags(ActivityFlags.GrantWriteUriPermission);
                        Intent targetIntent = Intent.CreateChooser(intent, "打开文件");
                        targetIntent.SetFlags(ActivityFlags.NewTask);
                        context.StartActivity(targetIntent);
                    }
                }
                catch(Exception e)
                {
                    System.Diagnostics.Debug.WriteLine("OpenFile === " + e.Message);
                }
            }                    
    View Code
  • 相关阅读:
    [bbk5153]第15集 Chapter 06 Working with Composite Data Types(Collection)
    [bbk5128]第12集 Chapter 06 Working with Composite Data Types 014998(Record)
    [bbk4998]第11集 Chapter 06 Working with Composite Data Types 004998(Record)
    [bbk4979]第06集 Chapter 04 Interacting with Oracle Database Server:SQL Statements in PL/SQL Programs(01)
    PHP与MYSQL事务处理
    Selenium封装
    pytest 失败重跑截图
    python 编码规范起源:PEP8 编码规范中文版
    pytest setup和teardown初始化
    谷歌浏览器linux,windows下载
  • 原文地址:https://www.cnblogs.com/devin_zhou/p/8520706.html
Copyright © 2011-2022 走看看