【通过FileProvider在应用间共享文件】

本文详细介绍了如何在Android应用中使用FileProvider处理7.0及以上版本的URI权限问题,包括在AndroidManifest.xml中的注册、file_paths.xml的配置,以及如何通过FileProvider7辅助类快速适配不同版本的intent.setData和setDataAndType方法,以实现拍照和APK安装的兼容性。
摘要由CSDN通过智能技术生成

使用content://替代file://,主要需要FileProvider的支持,而因为FileProvider是ContentProvider的子类,所以需要在AndroidManifest.xml中注册;而又因为需要对真实的filepath进行映射,所以需要编写一个xml文档,用于描述可使用的文件夹目录,以及通过name去映射该文件夹目录。

对于权限,有两种方式:

  • 方式一为Intent.addFlags,该方式主要用于针对intent.setData,setDataAndType以及setClipData相关方式传递uri的。
  • 方式二为grantUriPermission来进行授权

相比来说方式二较为麻烦,因为需要指定目标应用包名,很多时候并不清楚,所以需要通过PackageManager进行查找到所有匹配的应用,全部进行授权。不过更为稳妥~

方式一较为简单,对于intent.setData,setDataAndType正常使用即可,但是对于setClipData,由于5.0前后Intent#migrateExtraStreamToClipData,代码发生变化,需要注意

快速完成适配

(1)新建一个module

创建一个library的module,在其AndroidManifest.xml中完成FileProvider的注册,代码编写为:

 
  1. <application>

  2. <provider

  3. android:name="android.support.v4.content.FileProvider"

  4. android:authorities="${applicationId}.android7.fileprovider"

  5. android:exported="false"

  6. android:grantUriPermissions="true">

  7. <meta-data

  8. android:name="android.support.FILE_PROVIDER_PATHS"

  9. android:resource="@xml/file_paths" />

  10. </provider>

  11. </application>

注意一点,android:authorities不要写死,因为该library最终可能会让多个项目引用,而android:authorities是不可以重复的,如果两个app中定义了相同的,则后者无法安装到手机中(authority conflict)。

同样的的编写file_paths~

 
  1. <?xml version="1.0" encoding="utf-8"?>

  2. <paths xmlns:android="http://schemas.android.com/apk/res/android">

  3. <root-path

  4. name="root"

  5. path="" />

  6. <files-path

  7. name="files"

  8. path="" />

  9. <cache-path

  10. name="cache"

  11. path="" />

  12. <external-path

  13. name="external"

  14. path="" />

  15. <external-files-path

  16. name="external_file_path"

  17. path="" />

  18. <external-cache-path

  19. name="external_cache_path"

  20. path="" />

  21. </paths>

最后再编写一个辅助类,例如:

 
  1. public class FileProvider7 {

  2. public static Uri getUriForFile(Context context, File file) {

  3. Uri fileUri = null;

  4. if (Build.VERSION.SDK_INT >= 24) {

  5. fileUri = getUriForFile24(context, file);

  6. } else {

  7. fileUri = Uri.fromFile(file);

  8. }

  9. return fileUri;

  10. }

  11. public static Uri getUriForFile24(Context context, File file) {

  12. Uri fileUri = android.support.v4.content.FileProvider.getUriForFile(context,

  13. context.getPackageName() + ".android7.fileprovider",

  14. file);

  15. return fileUri;

  16. }

  17. public static void setIntentDataAndType(Context context,

  18. Intent intent,

  19. String type,

  20. File file,

  21. boolean writeAble) {

  22. if (Build.VERSION.SDK_INT >= 24) {

  23. intent.setDataAndType(getUriForFile(context, file), type);

  24. intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

  25. if (writeAble) {

  26. intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

  27. }

  28. } else {

  29. intent.setDataAndType(Uri.fromFile(file), type);

  30. }

  31. }

  32. }

可以根据自己的需求添加方法。

好了,这样我们的一个小库就写好了~~

(2)使用

如果哪个项目需要适配7.0,那么只需要这样引用这个库,然后只需要改动一行代码即可完成适配啦,例如:

拍照

 
  1. public void takePhotoNoCompress(View view) {

  2. Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

  3. if (takePictureIntent.resolveActivity(getPackageManager()) != null) {

  4. String filename = new SimpleDateFormat("yyyyMMdd-HHmmss", Locale.CHINA)

  5. .format(new Date()) + ".png";

  6. File file = new File(Environment.getExternalStorageDirectory(), filename);

  7. mCurrentPhotoPath = file.getAbsolutePath();

  8. Uri fileUri = FileProvider7.getUriForFile(this, file);

  9. takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

  10. startActivityForResult(takePictureIntent, REQUEST_CODE_TAKE_PHOTO);

  11. }

  12. }

只需要改动

 Uri fileUri = FileProvider7.getUriForFile(this, file);

即可。

安装apk

同样的修改setDataAndType为:

 
  1. FileProvider7.setIntentDataAndType(this,

  2. intent, "application/vnd.android.package-archive", file, true);

  • 17
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值