7.0fileprovider的使用

首先分三部分:

xml中配置需要共享的路径;manifest中配置这个fileprovider;代码中调用。 当然,对于项目中存在多个共享路径,就需要对fileprovider进行特殊处理


xml:

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

    <files-path/>代表的根目录: Context.getFilesDir()
    <external-path/>代表的根目录: Environment.getExternalStorageDirectory()
    <cache-path/>代表的根目录: getCacheDir()
manifest:

   <provider
            android:name=".MyProvider"
            android:authorities="${applicationId}.fileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
        </provider>
注意,这里的authorities,可以随便起,只要你调用时候写对就行了;对于name,本来应该是android.supportV4.FileProvider,但是由于项目中存在了一个,就不必须再新建一个类,继承FileProvider。
代码调用;

 File imagePath = new File( Environment.getExternalStorageDirectory(), "Download");
        File newFile = new File(imagePath, "test.png");
        Uri contentUri = FileProvider.getUriForFile(MainActivity.this, "com.yosemite.testconstarinlayout.fileProvider", newFile);
        Intent intent=new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(contentUri,"image/*");
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        startActivity(intent);




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值