当前位置:   article > 正文

android 分享文件功能实现

android 分享文件

Android面试题总结:
Android所有面试题总结
https://blog.csdn.net/gongjdde/category_11520676.html

Framework相关内容总结:

https://blog.csdn.net/gongjdde/category_10613658.html

Kotlin相关内容总结:

https://blog.csdn.net/gongjdde/category_10998720.html

分享功能原理是通过Intent将文件传给其他应用,代码如下:

  1. public static void shareFile(Context context, String fileName) {
  2. File file = new File(fileName);
  3. if (null != file && file.exists()) {
  4. Intent share = new Intent(Intent.ACTION_SEND);
  5. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  6. Uri contentUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileprovider",file);
  7. share.putExtra(Intent.EXTRA_STREAM, contentUri);
  8. share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  9. } else {
  10. share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
  11. }
  12. share.setType("application/vnd.ms-excel");//此处可发送多种文件
  13. share.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  14. share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  15. context.startActivity(Intent.createChooser(share, "分享文件"));
  16. } else {
  17. Toast.makeText(context, "分享文件不存在", Toast.LENGTH_SHORT).show();
  18. }
  19. }

如上所示是分享excel表格的代码,其中7.0以上做特殊处理,需要包裹uri,并且需要添加xml文件,如下所示:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <paths xmlns:android="http://schemas.android.com/apk/res/android">
  3. <!-- apk存放路径:getExternalCacheDir()/apk/xiaomaigui.apk -->
  4. <root-path name="root" path="system"/>
  5. <!--
  6. 如下路径是在:SDCard/Record/下面的文件
  7. -->
  8. <external-path
  9. name="Record"
  10. path="Record"/>
  11. </paths>

在AndroidManifest中的Application添加xml文件:

  1. <provider
  2. android:name="androidx.core.content.FileProvider"
  3. android:authorities="com.example.handscanner.fileprovider"
  4. android:exported="false"
  5. android:grantUriPermissions="true">
  6. <meta-data
  7. android:name="android.support.FILE_PROVIDER_PATHS"
  8. android:resource="@xml/file_paths" />
  9. </provider>

以上,即可完成分享excel表格的功能,如需分享其他文件,需要找到对应文件类型,如下是所有文件的类型:

  1. {".3gp", "video/3gpp"},
  2. {".apk", "application/vnd.android.package-archive"},
  3. {".asf", "video/x-ms-asf"},
  4. {".avi", "video/x-msvideo"},
  5. {".bin", "application/octet-stream"},
  6. {".bmp", "image/bmp"},
  7. {".c", "text/plain"},
  8. {".class", "application/octet-stream"},
  9. {".conf", "text/plain"},
  10. {".cpp", "text/plain"},
  11. {".doc", "application/msword"},
  12. {".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
  13. {".xls", "application/vnd.ms-excel"},
  14. {".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
  15. {".exe", "application/octet-stream"},
  16. {".gif", "image/gif"},
  17. {".gtar", "application/x-gtar"},
  18. {".gz", "application/x-gzip"},
  19. {".h", "text/plain"},
  20. {".htm", "text/html"},
  21. {".html", "text/html"},
  22. {".jar", "application/java-archive"},
  23. {".java", "text/plain"},
  24. {".jpeg", "image/jpeg"},
  25. {".jpg", "image/jpeg"},
  26. {".js", "application/x-javascript"},
  27. {".log", "text/plain"},
  28. {".m3u", "audio/x-mpegurl"},
  29. {".m4a", "audio/mp4a-latm"},
  30. {".m4b", "audio/mp4a-latm"},
  31. {".m4p", "audio/mp4a-latm"},
  32. {".m4u", "video/vnd.mpegurl"},
  33. {".m4v", "video/x-m4v"},
  34. {".mov", "video/quicktime"},
  35. {".mp2", "audio/x-mpeg"},
  36. {".mp3", "audio/x-mpeg"},
  37. {".mp4", "video/mp4"},
  38. {".mpc", "application/vnd.mpohun.certificate"},
  39. {".mpe", "video/mpeg"},
  40. {".mpeg", "video/mpeg"},
  41. {".mpg", "video/mpeg"},
  42. {".mpg4", "video/mp4"},
  43. {".mpga", "audio/mpeg"},
  44. {".msg", "application/vnd.ms-outlook"},
  45. {".ogg", "audio/ogg"},
  46. {".pdf", "application/pdf"},
  47. {".png", "image/png"},
  48. {".pps", "application/vnd.ms-powerpoint"},
  49. {".ppt", "application/vnd.ms-powerpoint"},
  50. {".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"},
  51. {".prop", "text/plain"},
  52. {".rc", "text/plain"},
  53. {".rmvb", "audio/x-pn-realaudio"},
  54. {".rtf", "application/rtf"},
  55. {".sh", "text/plain"},
  56. {".tar", "application/x-tar"},
  57. {".tgz", "application/x-compressed"},
  58. {".txt", "text/plain"},
  59. {".wav", "audio/x-wav"},
  60. {".wma", "audio/x-ms-wma"},
  61. {".wmv", "audio/x-ms-wmv"},
  62. {".wps", "application/vnd.ms-works"},
  63. {".xml", "text/plain"},
  64. {".z", "application/x-compress"},
  65. {".zip", "application/x-zip-compressed"},
  66. {"", "*/*"}

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/821881
推荐阅读
相关标签
  

闽ICP备14008679号