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

作者:林恒 时间:2022-06-12 18:11:58 

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

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

在AndroidManifest.xml注册ACTION事件

<activity
       android:name="com.test.app.MainActivity"
       android:configChanges="orientation|keyboardHidden|screenSize"
       android:label="这里的名称会对外显示"
       android:launchMode="singleTask"
       android:screenOrientation="portrait">
       //注册接收分享
       <intent-filter>
           <action android:name="android.intent.action.SEND" />
           <category android:name="android.intent.category.DEFAULT" />

//接收分享的文件类型
           <data android:mimeType="image/*" />
           <data android:mimeType="application/msword" />
           <data android:mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.document" />
           <data android:mimeType="application/vnd.ms-excel" />
           <data android:mimeType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
           <data android:mimeType="application/vnd.ms-powerpoint" />
           <data android:mimeType="application/vnd.openxmlformats-officedocument.presentationml.presentation" />
           <data android:mimeType="application/pdf" />
           <data android:mimeType="text/plain" />
           </intent-filter>

//注册默认打开事件,微信、QQ的其他应用打开
       <intent-filter>
           <action android:name="android.intent.action.VIEW" />
           <category android:name="android.intent.category.DEFAULT" />

//接收打开的文件类型
           <data android:scheme="file" />
           <data android:scheme="content" />
           <data android:mimeType="image/*" />
           <data android:mimeType="application/msword" />
           <data android:mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.document" />
           <data android:mimeType="application/vnd.ms-excel" />
           <data android:mimeType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
           <data android:mimeType="application/vnd.ms-powerpoint" />
           <data android:mimeType="application/vnd.openxmlformats-officedocument.presentationml.presentation" />
           <data android:mimeType="application/pdf" />
           <data android:mimeType="text/plain" />
           </intent-filter>
       </activity>

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

  • 当APP进程在后台时,会调用Activity的onNewIntent方法

  • 当APP进程被杀死时,会调用onCreate方法

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

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   receiveActionSend(intent);
}

@Override
protected void onNewIntent(Intent intent) {
   super.onNewIntent(intent);
   receiveActionSend(intent);
}

receiveActionSend方法如下

public void receiveActionSend(Intent intent) {
   String action = intent.getAction();
   String type = intent.getType();

//判断action事件
   if (type == null || (!Intent.ACTION_VIEW.equals(action) && !Intent.ACTION_SEND.equals(action))) {
       return;
   }

//取出文件uri
   Uri uri = intent.getData();
   if (uri == null) {
       uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
   }

//获取文件真实地址
   String filePath = UriUtils.getFileFromUri(EdusohoApp.baseApp, uri);
   if (TextUtils.isEmpty(filePath)) {
       return;
   }

//业务处理
   .
   .
   .
}

获取真实路径getFileFromUri方法

/**
* 获取真实路径
*
* @param context
*/
public static String getFileFromUri(Context context, Uri uri) {
   if (uri == null) {
       return null;
   }
   switch (uri.getScheme()) {
       case ContentResolver.SCHEME_CONTENT:
           //Android7.0之后的uri content:// URI
           return getFilePathFromContentUri(context, uri);
       case ContentResolver.SCHEME_FILE:
       default:
           //Android7.0之前的uri file://
           return new File(uri.getPath()).getAbsolutePath();
   }
}

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到自己的私有目录。
方法如下:

/**
* 从uri获取path
*
* @param uri content://media/external/file/109009
*            <p>
*            FileProvider适配
*            content://com.tencent.mobileqq.fileprovider/external_files/storage/emulated/0/Tencent/QQfile_recv/
*            content://com.tencent.mm.external.fileprovider/external/tencent/MicroMsg/Download/
*/
private static String getFilePathFromContentUri(Context context, Uri uri) {
   if (null == uri) return null;
   String data = null;

String[] filePathColumn = {MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME};
   Cursor cursor = context.getContentResolver().query(uri, filePathColumn, null, null, null);
   if (null != cursor) {
       if (cursor.moveToFirst()) {
           int index = cursor.getColumnIndex(MediaStore.MediaColumns.DATA);
           if (index > -1) {
               data = cursor.getString(index);
           } else {
               int nameIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
               String fileName = cursor.getString(nameIndex);
               data = getPathFromInputStreamUri(context, uri, fileName);
           }
       }
       cursor.close();
   }
   return data;
}

/**
* 用流拷贝文件一份到自己APP私有目录下
*
* @param context
* @param uri
* @param fileName
*/
private static String getPathFromInputStreamUri(Context context, Uri uri, String fileName) {
   InputStream inputStream = null;
   String filePath = null;

if (uri.getAuthority() != null) {
       try {
           inputStream = context.getContentResolver().openInputStream(uri);
           File file = createTemporalFileFrom(context, inputStream, fileName);
           filePath = file.getPath();

} catch (Exception e) {
       } finally {
           try {
               if (inputStream != null) {
                   inputStream.close();
               }
           } catch (Exception e) {
           }
       }
   }

return filePath;
}

private static File createTemporalFileFrom(Context context, InputStream inputStream, String fileName)
       throws IOException {
   File targetFile = null;

if (inputStream != null) {
       int read;
       byte[] buffer = new byte[8 * 1024];
       //自己定义拷贝文件路径
       targetFile = new File(context.getExternalCacheDir(), fileName);
       if (targetFile.exists()) {
           targetFile.delete();
       }
       OutputStream outputStream = new FileOutputStream(targetFile);

while ((read = inputStream.read(buffer)) != -1) {
           outputStream.write(buffer, 0, read);
       }
       outputStream.flush();

try {
           outputStream.close();
       } catch (IOException e) {
           e.printStackTrace();
       }
   }

return targetFile;
}

来源:https://www.cnblogs.com/smileZAZ/archive/2022/11/07/16866720.html

标签:android,接收,第三方,分享
0
投稿

猜你喜欢

  • C#如何用ThoughtWorks生成二维码

    2022-09-28 20:44:53
  • Java StringUtils字符串分割转数组的实现

    2023-07-19 12:43:37
  • 判断一个整数是否是2的N次幂实现方法

    2022-12-25 00:55:10
  • 使用Spring Boot AOP处理方法的入参和返回值

    2022-03-02 07:25:41
  • Android 在 res/layout 文件夹 下创建一个 子文件夹实例

    2021-07-31 22:50:21
  • Android自定义view实现滑动解锁九宫格控件

    2021-12-28 02:24:16
  • c#通过xpath读取xml示例

    2023-07-16 01:31:05
  • Java 面试题和答案 -(上)

    2023-10-08 08:15:56
  • 深入理解Java注解类型(@Annotation)

    2022-11-14 17:28:42
  • Android之获取手机内部及sdcard存储空间的方法

    2022-11-11 13:10:18
  • Java动态 代理的应用详解

    2023-11-25 08:15:24
  • SpringCloud OpenFeign 服务调用传递 token的场景分析

    2022-12-26 22:24:07
  • 详解Java token主流框架之JWT

    2022-03-30 19:30:34
  • java实现斗地主游戏

    2022-02-18 14:06:26
  • JAVA 获取系统当前时间实例代码

    2022-09-01 05:53:01
  • Android截屏保存png图片的实例代码

    2022-01-26 16:10:11
  • 详解Java七大阻塞队列之SynchronousQueue

    2023-12-16 04:21:30
  • C#网络编程中常用特性介绍

    2021-09-03 23:07:51
  • C# 图片剪切与缩小的实例

    2021-12-31 14:32:53
  • C# Winform中如何绘制动画示例详解

    2022-03-28 13:26:26
  • asp之家 软件编程 m.aspxhome.com