Android快速分享文件/文字到QQ/微信

一般来说,要想分享文件/文字到微信朋友圈、QQ,都是需要接入微信、QQ的分享sdk;不过也有个快速分享到QQ、微信的小窍门,就是通过action 为“android.intent.action.SEND”的Intent。
Talk is cheap. Show me the code.

1、先看看手Q的AndroidManifest.xml,我们可以看到可以接受“android.intent.action.SEND”的Intent,mimeType可以是图片("image/*")视频(“video/*”)音频("audio/*")文本("text/*")各种二进制文件("application/*")

<activity android:configChanges="keyboardHidden|locale|orientation" android:icon="@drawable/name_APKTOOL_DUPLICATENAME_0x7f021750" android:name="cooperation.qlink.QlinkShareJumpActivity" android:screenOrientation="portrait" android:theme="@android:style/Theme.Translucent">
   <intent-filter android:label="@string/name_APKTOOL_DUPLICATENAME_0x7f0c1126">
       <action android:name="android.intent.action.SEND"/>
       <category android:name="android.intent.category.DEFAULT"/>
       <data android:mimeType="image/*"/>
       <data android:mimeType="video/*"/>
       <data android:mimeType="audio/*"/>
       <data android:mimeType="application/*"/>
   </intent-filter>
   <intent-filter android:label="@string/name_APKTOOL_DUPLICATENAME_0x7f0c1126">
       <action android:name="android.intent.action.SEND_MULTIPLE"/>
       <category android:name="android.intent.category.DEFAULT"/>
       <data android:mimeType="image/*"/>
       <data android:mimeType="video/*"/>
       <data android:mimeType="audio/*"/>
       <data android:mimeType="application/*"/>
   </intent-filter>
</activity>
<activity android:configChanges="keyboardHidden|locale|orientation" android:exported="true" android:icon="@drawable/name_APKTOOL_DUPLICATENAME_0x7f02153d" android:name=".activity.qfileJumpActivity" android:theme="@android:style/Theme.Translucent">
    <intent-filter android:label="@string/name_APKTOOL_DUPLICATENAME_0x7f0c00fe">
        <action android:name="android.intent.action.SEND"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="image/*"/>
        <data android:mimeType="video/*"/>
        <data android:mimeType="audio/*"/>
        <data android:mimeType="application/*"/>
        <data android:mimeType="text/*"/>
    </intent-filter>
    <intent-filter android:label="@string/name_APKTOOL_DUPLICATENAME_0x7f0c00fe">
         <action android:name="android.intent.action.SEND_MULTIPLE"/>
         <category android:name="android.intent.category.DEFAULT"/>
         <data android:mimeType="image/*"/>
     </intent-filter>
 </activity>

2、再来看看微信的AndroidManifest.xml, 我们也可以看到有Activity接受action为“android.intent.action.SEND”的Intent,接受的mimeType类型同上

<activity android:configChanges="keyboardHidden|orientation|screenSize" android:icon="@drawable/icon" android:name="com.tencent.mm.ui.tools.ShareImgUI" android:theme="@style/ab">
     <intent-filter android:label="@string/v1">
         <action android:name="android.intent.action.SEND"/>
         <category android:name="android.intent.category.DEFAULT"/>
         <data android:mimeType="image/*"/>
         <data android:mimeType="video/*"/>
         <data android:mimeType="text/*"/>
         <data android:mimeType="application/*"/>
     </intent-filter>
     <intent-filter android:label="@string/v1">
         <action android:name="android.intent.action.SEND_MULTIPLE"/>
         <category android:name="android.intent.category.DEFAULT"/>
         <data android:mimeType="image/*"/>
     </intent-filter>
</activity>
<activity android:configChanges="keyboardHidden|orientation|screenSize" android:icon="@drawable/b1f" android:name="com.tencent.mm.ui.tools.AddFavoriteUI" android:theme="@style/ab">
    <intent-filter android:label="@string/gb">
          <action android:name="android.intent.action.SEND"/>
          <category android:name="android.intent.category.DEFAULT"/>
          <data android:mimeType="image/*"/>
          <data android:mimeType="video/*"/>
          <data android:mimeType="text/*"/>
          <data android:mimeType="application/*"/>
          <data android:mimeType="audio/*"/>
    </intent-filter>
    <intent-filter android:label="@string/gb">
         <action android:name="android.intent.action.SEND_MULTIPLE"/>
         <category android:name="android.intent.category.DEFAULT"/>
         <data android:mimeType="image/*"/>
     </intent-filter>
</activity>

3、那么,我们想要分享文件到微信/QQ/邮件就可以设置代码如下:

Intent intent = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_SUBJECT," 图片分享" );
// 兼容android 7.0+
Uri uri = FileProvider.getUriForFile(
          getContext(),
          getContext().getPackageName(),
          barcodeFile);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType("image/*");
startActivity(intent);

4、看看效果,完美:

Android快速分享文件/文字到QQ/微信_第1张图片

你可能感兴趣的:(android)