AndroidN FileProvider使用

2020-09-03  本文已影响0人  美晨菌
1. AndroidManifest.xml
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.android.dialer.files"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>
2. file_paths.xml
<paths xmlns:tools="http://schemas.android.com/tools"
    tools:ignore="UnusedResources">
  <!-- Offer access to files under Context.getCacheDir() -->
  <cache-path name="my_cache"/>
  <!-- Offer access to files under Context.getExternalCacheDir() -->
  <external-path path="." name="external_storage_root" />
  <!-- Offer access to voicemail folder under Context.getFilesDir() -->
  <files-path
    name="voicemails"
    path="voicemails/"/>
</paths>
3. 可以生成一个其他应用访问的Uri
        String photoPath = getFilesDir() + "/Pictures/";
        Intent intent  = new Intent();
        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        Uri uri = FileProvider.getUriForFile(this, "com.tct.rxproject.provider", new File(photoPath + "aaa.jpg"));
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION
                | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        startActivityForResult(intent, 0);
上一篇下一篇

猜你喜欢

热点阅读