• Android 接收微信、QQ其他应用打开,第三方分享


    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助

    在AndroidManifest.xml注册ACTION事件

    1. <activity
    2. android:name="com.test.app.MainActivity"
    3. android:configChanges="orientation|keyboardHidden|screenSize"
    4. android:label="这里的名称会对外显示"
    5. android:launchMode="singleTask"
    6. android:screenOrientation="portrait">
    7. //注册接收分享
    8. <intent-filter>
    9. <action android:name="android.intent.action.SEND" />
    10. <category android:name="android.intent.category.DEFAULT" />
    11. //接收分享的文件类型
    12. <data android:mimeType="image/*" />
    13. <data android:mimeType="application/msword" />
    14. <data android:mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.document" />
    15. <data android:mimeType="application/vnd.ms-excel" />
    16. <data android:mimeType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
    17. <data android:mimeType="application/vnd.ms-powerpoint" />
    18. <data android:mimeType="application/vnd.openxmlformats-officedocument.presentationml.presentation" />
    19. <data android:mimeType="application/pdf" />
    20. <data android:mimeType="text/plain" />
    21. </intent-filter>
    22. //注册默认打开事件,微信、QQ的其他应用打开
    23. <intent-filter>
    24. <action android:name="android.intent.action.VIEW" />
    25. <category android:name="android.intent.category.DEFAULT" />
    26. //接收打开的文件类型
    27. <data android:scheme="file" />
    28. <data android:scheme="content" />
    29. <data android:mimeType="image/*" />
    30. <data android:mimeType="application/msword" />
    31. <data android:mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.document" />
    32. <data android:mimeType="application/vnd.ms-excel" />
    33. <data android:mimeType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
    34. <data android:mimeType="application/vnd.ms-powerpoint" />
    35. <data android:mimeType="application/vnd.openxmlformats-officedocument.presentationml.presentation" />
    36. <data android:mimeType="application/pdf" />
    37. <data android:mimeType="text/plain" />
    38. </intent-filter>
    39. </activity>

    在用于接收分享的Activity里面加接收代码

    1. 当APP进程在后台时,会调用Activity的onNewIntent方法
    2. 当APP进程被杀死时,会调用onCreate方法

    所以在两个方法中都需要监听事件

    1. @Override
    2. protected void onCreate(@Nullable Bundle savedInstanceState) {
    3. super.onCreate(savedInstanceState);
    4. receiveActionSend(intent);
    5. }
    6. @Override
    7. protected void onNewIntent(Intent intent) {
    8. super.onNewIntent(intent);
    9. receiveActionSend(intent);
    10. }

    receiveActionSend方法如下

    1. public void receiveActionSend(Intent intent) {
    2. String action = intent.getAction();
    3. String type = intent.getType();
    4. //判断action事件
    5. if (type == null || (!Intent.ACTION_VIEW.equals(action) && !Intent.ACTION_SEND.equals(action))) {
    6. return;
    7. }
    8. //取出文件uri
    9. Uri uri = intent.getData();
    10. if (uri == null) {
    11. uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
    12. }
    13. //获取文件真实地址
    14. String filePath = UriUtils.getFileFromUri(EdusohoApp.baseApp, uri);
    15. if (TextUtils.isEmpty(filePath)) {
    16. return;
    17. }
    18. //业务处理
    19. .
    20. .
    21. .
    22. }

    获取真实路径getFileFromUri方法

    1. /**
    2. * 获取真实路径
    3. *
    4. * @param context
    5. */
    6. public static String getFileFromUri(Context context, Uri uri) {
    7. if (uri == null) {
    8. return null;
    9. }
    10. switch (uri.getScheme()) {
    11. case ContentResolver.SCHEME_CONTENT:
    12. //Android7.0之后的uri content:// URI
    13. return getFilePathFromContentUri(context, uri);
    14. case ContentResolver.SCHEME_FILE:
    15. default:
    16. //Android7.0之前的uri file://
    17. return new File(uri.getPath()).getAbsolutePath();
    18. }
    19. }

    Android7.0之后的uri content:// URI需要对微信、QQ等第三方APP做兼容

    • 在文件管理选择本应用打开时,url的值为content://media/external/file/85139
    • 在微信中选择本应用打开时,url的值为 content://com.tencent.mm.external.fileprovider/external/tencent/MicroMsg/Download/111.doc
    • 在QQ中选择本应用打开时,url的值为 content://com.tencent.mobileqq.fileprovider/external_files/storage/emulated/0/Tencent/QQfile_recv/

    第一种为系统统一文件资源,能通过系统方法转化为绝对路径;
    微信、QQ的为fileProvider,只能获取到文件流,需要先将文件copy到自己的私有目录。
    方法如下:

    1. /**
    2. * 从uri获取path
    3. *
    4. * @param uri content://media/external/file/109009
    5. *

    6. * FileProvider适配
    7. * content://com.tencent.mobileqq.fileprovider/external_files/storage/emulated/0/Tencent/QQfile_recv/
    8. * content://com.tencent.mm.external.fileprovider/external/tencent/MicroMsg/Download/
    9. */
    10. private static String getFilePathFromContentUri(Context context, Uri uri) {
    11. if (null == uri) return null;
    12. String data = null;
    13. String[] filePathColumn = {MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME};
    14. Cursor cursor = context.getContentResolver().query(uri, filePathColumn, null, null, null);
    15. if (null != cursor) {
    16. if (cursor.moveToFirst()) {
    17. int index = cursor.getColumnIndex(MediaStore.MediaColumns.DATA);
    18. if (index > -1) {
    19. data = cursor.getString(index);
    20. } else {
    21. int nameIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
    22. String fileName = cursor.getString(nameIndex);
    23. data = getPathFromInputStreamUri(context, uri, fileName);
    24. }
    25. }
    26. cursor.close();
    27. }
    28. return data;
    29. }
    30. /**
    31. * 用流拷贝文件一份到自己APP私有目录下
    32. *
    33. * @param context
    34. * @param uri
    35. * @param fileName
    36. */
    37. private static String getPathFromInputStreamUri(Context context, Uri uri, String fileName) {
    38. InputStream inputStream = null;
    39. String filePath = null;
    40. if (uri.getAuthority() != null) {
    41. try {
    42. inputStream = context.getContentResolver().openInputStream(uri);
    43. File file = createTemporalFileFrom(context, inputStream, fileName);
    44. filePath = file.getPath();
    45. } catch (Exception e) {
    46. } finally {
    47. try {
    48. if (inputStream != null) {
    49. inputStream.close();
    50. }
    51. } catch (Exception e) {
    52. }
    53. }
    54. }
    55. return filePath;
    56. }
    57. private static File createTemporalFileFrom(Context context, InputStream inputStream, String fileName)
    58. throws IOException {
    59. File targetFile = null;
    60. if (inputStream != null) {
    61. int read;
    62. byte[] buffer = new byte[8 * 1024];
    63. //自己定义拷贝文件路径
    64. targetFile = new File(context.getExternalCacheDir(), fileName);
    65. if (targetFile.exists()) {
    66. targetFile.delete();
    67. }
    68. OutputStream outputStream = new FileOutputStream(targetFile);
    69. while ((read = inputStream.read(buffer)) != -1) {
    70. outputStream.write(buffer, 0, read);
    71. }
    72. outputStream.flush();
    73. try {
    74. outputStream.close();
    75. } catch (IOException e) {
    76. e.printStackTrace();
    77. }
    78. }
    79. return targetFile;
    80. }

    如果对您有所帮助,欢迎您点个关注,我会定时更新技术文档,大家一起讨论学习,一起进步。

     

  • 相关阅读:
    【Paper】2021_有向间歇通讯下多智能体系统的一致性
    开发知识点-uniapp微信小程序-开发指南
    Python Flask框架(二)Flask与HTTP
    通过CRM软件系统赢得销售机会的五大原则
    c++ | makefile | 编译 | 链接库
    剑指offer62-67排序-前缀树
    YOLOv7改进:新颖的上下文解耦头TSCODE,即插即用,各个数据集下实现暴力涨点
    大数据可视化——Sqoop与Hive的安装详解
    SfM——八点法计算F矩阵(基础矩阵)与三角测量
    前端架构师之01_ES6_基础
  • 原文地址:https://blog.csdn.net/qq_40716795/article/details/127738057