美文网首页
AndroidN FileProvider使用

AndroidN FileProvider使用

作者: 美晨菌 | 来源:发表于2020-09-03 14:20 被阅读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);
    

    相关文章

      网友评论

          本文标题:AndroidN FileProvider使用

          本文链接:https://www.haomeiwen.com/subject/kccgsktx.html