Android 系統跳轉實現分享功能(如 微信 朋友圈 QQ QQ空間 微博,支持多圖分享)

? CSDN地址:?https://blog.csdn.net/shuai497331206


?前者基本都是一個實現思路,? 唯獨微博實現比較特殊一點。

? ? ?我對兩個類做了封裝? 只要配置沒問題虐急,?拿去直接用箱残。

? ? ?先來一波配置

?清單文件下

android:name="androidx.core.content.FileProvider"

android:authorities="包名.fileprovider"

android:exported="false"

android:grantUriPermissions="true">

android:name="android.support.FILE_PROVIDER_PATHS"

android:resource="@xml/file_paths_public"/>


下的文件 Context.getExternalCacheDir目錄下的目錄-->

name="external_cache_path"

path="."/>

<!--配置root-path滔迈。這樣子可以讀取到sd卡和一些應用分身的目錄,否則微信分身保存的圖片被辑,就會導致 java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/999/tencent/MicroMsg/WeiXin/export1544062754693.jpg燎悍,在小米6的手機上微信分身有這個crash,華為沒有

-->

name="root-path"

path=""/>

? ? ...


? ?(3)?network_security_config


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


? ?因為是需要內容提供者?跳轉三方應用的頁面? 所以必須要添加盼理,?還有就是?android7.0以后的權限


保存圖片下載到本地

package com.ggmall.ggb.personal.utils;

import android.content.Context;

import android.content.Intent;

import android.content.pm.PackageInfo;

import android.content.pm.PackageManager;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.net.Uri;

import android.os.Environment;

import android.util.Log;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.URL;

import java.util.Calendar;

import java.util.List;

/**

* Created by ShinnyYang on 2020/2/26.

*/

public class Tools {

? ? public static String IMAGE_NAME = "iv_share_";

? ? public static int? i = 0;

? ? //根據網絡圖片url路徑保存到本地

? ? public static final File saveImageToSdCard(Context context, String image) {

? ? ? ? boolean success = false;

? ? ? ? File file = null;

? ? ? ? try {

? ? ? ? ? ? file = createStableImageFile(context);

? ? ? ? ? ? Bitmap bitmap = null;

? ? ? ? ? ? URL url = new URL(image);

? ? ? ? ? ? HttpURLConnection conn = null;

? ? ? ? ? ? conn = (HttpURLConnection) url.openConnection();

? ? ? ? ? ? InputStream is = null;

? ? ? ? ? ? is = conn.getInputStream();

? ? ? ? ? ? bitmap =? BitmapFactory.decodeStream(is);

? ? ? ? ? ? FileOutputStream outStream;

? ? ? ? ? ? outStream = new FileOutputStream(file);

? ? ? ? ? ? bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);

? ? ? ? ? ? outStream.flush();

? ? ? ? ? ? outStream.close();

? ? ? ? ? ? success = true;

? ? ? ? } catch (Exception e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? ? ? if (success) {

? ? ? ? ? ? Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);

? ? ? ? ? ? Uri contentUri = Uri.fromFile(file);

? ? ? ? ? ? mediaScanIntent.setData(contentUri);

? ? ? ? ? ? context.sendBroadcast(mediaScanIntent);

? ? ? ? ? ? return file;

? ? ? ? } else {

? ? ? ? ? ? return null;

? ? ? ? }

? ? }

? ? //創(chuàng)建本地保存路徑

? ? public static File createStableImageFile(Context context) throws IOException {

? ? ? ? i++;

? ? ? ? String imageFileName =IMAGE_NAME + Calendar.getInstance().getTimeInMillis() + ".jpg";

? ? ? ? File storageDir = new File(Environment.getExternalStorageDirectory(),

? ? ? ? ? ? ? ? "DCIM");

//? ? ? ? File storageDir = new File(context.getExternalCacheDir() + "shareImg");

//? ? ? ? File storageDir = new File(Environment.getExternalStorageDirectory() + "/");

? ? ? ? Log.i("info","=======保存路徑====" + storageDir.getAbsolutePath());

? ? ? ? if (!storageDir.exists()){

? ? ? ? ? ? storageDir.mkdirs();

? ? ? ? }

? ? ? ? File image = new File(storageDir, imageFileName);

? ? ? ? return image;

? ? }

? ? //判斷是否安裝了微信,QQ,QQ空間

? ? public static boolean isAppAvilible(Context context,String mType) {

? ? ? ? final PackageManager packageManager = context.getPackageManager();// 獲取packagemanager

Listpinfo = packageManager.getInstalledPackages(0);// 獲取所有已安裝程序的包信息

? ? ? ? if (pinfo != null) {

for (int i = 0; i

Stringpn=pinfo.get(i).packageName;

if(pn.equals(mType)) {

returntrue;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }

returnfalse;

? ? }

publicstaticvoiddeletePic(Filefile){

if(file.isDirectory()){

File[]files=file.listFiles();

for(intj=0;j

Filef=files[j];

deletePic(f);

? ? ? ? ? ? }

//file.delete();//如要保留文件夾谈山,只刪除文件,請注釋這行

}else{

file.delete();

? ? ? ? }

? ? }

}


? ?分享的主要部分宏怔,


package com.ggmall.ggb.personal.utils;

import android.content.ComponentName;

import android.content.Context;

import android.content.Intent;

import android.content.pm.PackageInfo;

import android.content.pm.PackageManager;

import android.net.Uri;

import android.widget.Toast;

import java.io.File;

import java.io.FileNotFoundException;

import java.util.ArrayList;

import java.util.List;

/**

* Created by ShinnyYang on 2020/2/24.

*/

public class ShareUtils {

? ? /**

? ? * 微信7.0版本號奏路,兼容處理微信7.0版本分享到朋友圈不支持多圖片的問題

? ? */

? ? private static final int VERSION_CODE_FOR_WEI_XIN_VER7 = 1380;

? ? /**

? ? * 微信包名

? ? */

? ? public static final String PACKAGE_NAME_WEI_XIN = "com.tencent.mm";

? ? public static ShareUtils shareUtils = null;

? ? private Context context = null;

private Listfiles = new ArrayList<>();

? ? public static ShareUtils Initialize() {

? ? ? ? if (shareUtils == null) {

? ? ? ? ? ? shareUtils = new ShareUtils();

? ? ? ? }

? ? ? ? return shareUtils;

? ? }

? ? public ShareUtils setContext(Context context) {

? ? ? ? this.context = context;

? ? ? ? files.clear();

? ? ? ? return shareUtils;

? ? }

? ? public void shareQQ(String[] url) {

? ? ? ? if (!Tools.isAppAvilible(context, "com.tencent.mobileqq")) {

? ? ? ? ? ? Toast.makeText(context, "您還沒有安裝QQ", Toast.LENGTH_SHORT).show();

? ? ? ? ? ? return;

? ? ? ? }

? ? ? ? ShareSave(url, 3);

? ? }

? ? public void shareWeiXin(final String[] url) {

? ? ? ? if (!Tools.isAppAvilible(context, "com.tencent.mm")) {

? ? ? ? ? ? Toast.makeText(context, "您還沒有安裝微信", Toast.LENGTH_SHORT).show();

? ? ? ? ? ? return;

? ? ? ? }

? ? ? ? ShareSave(url, 0);

? ? }

? ? public void shareQZone(String[] url) {

? ? ? ? if (!Tools.isAppAvilible(context, "com.tencent.mobileqq")) {

? ? ? ? ? ? Toast.makeText(context, "您還沒有安裝QQ", Toast.LENGTH_SHORT).show();

? ? ? ? ? ? return;

? ? ? ? }

? ? ? ? ShareSave(url, 2);

? ? }

? ? public void sharePYQ(String[] url) {

? ? ? ? if (!Tools.isAppAvilible(context, "com.tencent.mm")) {

? ? ? ? ? ? Toast.makeText(context, "您還沒有安裝微信", Toast.LENGTH_SHORT).show();

? ? ? ? ? ? return;

? ? ? ? }

? ? ? ? ShareSave(url, 1);

? ? }

? ? public void shareWB(final String[] url) {

? ? ? ? if (!Tools.isAppAvilible(context, "com.sina.weibo")) {

? ? ? ? ? ? Toast.makeText(context, "您還沒有安裝微博", Toast.LENGTH_SHORT).show();

? ? ? ? ? ? return;

? ? ? ? }

? ? ? ? ShareSave(url, 4);

? ? }

? ? public void ShareSave(final String[] url, final int type) {

? ? ? ? new Thread(new Runnable() {

? ? ? ? ? ? @Override

? ? ? ? ? ? public void run() {

for (int i = 0; i

Filefile=null;

if(url[i].contains("http")) {

file=Tools.saveImageToSdCard(context,url[i]);

}else{

file=newFile(url[i]);

? ? ? ? ? ? ? ? ? ? }

files.add(file);

? ? ? ? ? ? ? ? }

ArrayListimageUris = new ArrayList();

? ? ? ? ? ? ? ? ? ? for (File f : files) {

? ? ? ? ? ? ? ? ? ? ? ? imageUris.add(Uri.fromFile(f));

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? if (type == 0) {

? ? ? ? ? ? ? ? ? ? shareWXSomeImg(context, imageUris);

? ? ? ? ? ? ? ? } else if (type == 1) {

? ? ? ? ? ? ? ? ? ? shareweipyqSomeImg(context, imageUris);

? ? ? ? ? ? ? ? } else if (type == 2) {

? ? ? ? ? ? ? ? ? ? shareQZoneImg(context, imageUris);

? ? ? ? ? ? ? ? } else if (type == 3) {

? ? ? ? ? ? ? ? ? ? shareQQImg(context, imageUris);

? ? ? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? ? ? shareWBImg(context, imageUris);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }).start();

? ? }

private void shareweipyqSomeImg(final Context context, ArrayListuri) {

? ? ? ? Intent shareIntent = new Intent();

? ? ? ? shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

? ? ? ? //2添加圖片數組

ArrayListimageUris = new ArrayList<>();

for (int i = 0; i

Uriurl=null;

try{

url=Uri.parse(android.provider.MediaStore.Images.

Media.insertImage(context.getContentResolver(),

files.get(i).getAbsolutePath(),files.get(i).getName(),null));

}catch(FileNotFoundExceptione) {

e.printStackTrace();

? ? ? ? ? ? }

imageUris.add(url);

? ? ? ? }

if(getVersionCode(context,PACKAGE_NAME_WEI_XIN)

// 微信7.0以下版本

shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);

shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,imageUris);

}else{

// 微信7.0及以上版本,朋友圈只支持單張圖片分享

shareIntent.setAction(Intent.ACTION_SEND);

shareIntent.putExtra(Intent.EXTRA_STREAM,imageUris.get(0));

? ? ? ? }

shareIntent.setType("image/*");

//3指定選擇微信

ComponentNamecomponentName=newComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");

shareIntent.setComponent(componentName);

//4開始分享

context.startActivity(Intent.createChooser(shareIntent, "分享圖片"));

? ? }

? ? /**

? ? * 拉起微信發(fā)送多張圖片給好友

? ? */

privatevoidshareWXSomeImg(Contextcontext,ArrayListuri) {

? ? ? ? Intent shareIntent = new Intent();

? ? ? ? //1調用系統分析

? ? ? ? shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);

? ? ? ? shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

? ? ? ? //2添加圖片數組

ArrayListimageUris = new ArrayList<>();

for (int i = 0; i

Uriurl=null;

try{

url=Uri.parse(android.provider.MediaStore.Images.

Media.insertImage(context.getContentResolver(),

files.get(i).getAbsolutePath(),files.get(i).getName(),null));

}catch(FileNotFoundExceptione) {

e.printStackTrace();

? ? ? ? ? ? }

imageUris.add(url);

? ? ? ? }

shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,imageUris);

shareIntent.setType("image/*");

//3指定選擇微信

ComponentNamecomponentName=newComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");

shareIntent.setComponent(componentName);

//4開始分享

context.startActivity(Intent.createChooser(shareIntent, "分享圖片"));

? ? }

? ? /**

* 拉起QQ發(fā)送多張圖片給好友

? ? */

privatevoidshareQQImg(Contextcontext,ArrayListuri) {

? ? ? ? Intent shareIntent = new Intent();

? ? ? ? //1調用系統分析

? ? ? ? shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);

? ? ? ? shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

? ? ? ? //2添加圖片數組

ArrayListimageUris = new ArrayList<>();

for (int i = 0; i

Uriurl=null;

try{

url=Uri.parse(android.provider.MediaStore.Images.

Media.insertImage(context.getContentResolver(),

files.get(i).getAbsolutePath(),files.get(i).getName(),null));

}catch(FileNotFoundExceptione) {

e.printStackTrace();

? ? ? ? ? ? }

imageUris.add(url);

? ? ? ? }

shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,imageUris);

shareIntent.setType("image/*");

//3指定選擇微信

ComponentNamecomponentName=newComponentName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");

shareIntent.setComponent(componentName);

//4開始分享

context.startActivity(Intent.createChooser(shareIntent, "分享圖片"));

? ? }

? ? /**

* 拉起QQ發(fā)送多張圖片給好友

? ? */

privatevoidshareQZoneImg(Contextcontext,ArrayListuri) {

? ? ? ? Intent shareIntent = new Intent();

? ? ? ? //1調用系統分析

? ? ? ? shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);

? ? ? ? shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

? ? ? ? //2添加圖片數組

ArrayListimageUris = new ArrayList<>();

for (int i = 0; i

Uriurl=null;

try{

url=Uri.parse(android.provider.MediaStore.Images.

Media.insertImage(context.getContentResolver(),

files.get(i).getAbsolutePath(),files.get(i).getName(),null));

}catch(FileNotFoundExceptione) {

e.printStackTrace();

? ? ? ? ? ? }

imageUris.add(url);

? ? ? ? }

shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,imageUris);

shareIntent.setType("image/*");

//3指定選擇微信

ComponentNamecomponentName=newComponentName("com.qzone", "com.qzonex.module.operation.ui.QZonePublishMoodActivity");

shareIntent.setComponent(componentName);

//4開始分享

context.startActivity(Intent.createChooser(shareIntent, "分享圖片"));

? ? }

? ? /**

? ? * 拉起微博發(fā)送多張圖片給好友

? ? */

privatevoidshareWBImg(Contextcontext,ArrayListuri) {

? ? ? ? Intent shareIntent = new Intent();

? ? ? ? //1調用系統分析

? ? ? ? shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);

? ? ? ? shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

? ? ? ? //2添加圖片數組

ArrayListimageUris = new ArrayList<>();

for (int i = 0; i

Uriurl=null;

try{

url=Uri.parse(android.provider.MediaStore.Images.

Media.insertImage(context.getContentResolver(),

files.get(i).getAbsolutePath(),files.get(i).getName(),null));

}catch(FileNotFoundExceptione) {

e.printStackTrace();

? ? ? ? ? ? }

imageUris.add(url);

? ? ? ? }

shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,imageUris);

shareIntent.setType("image/*");

//3指定選擇微信

//ComponentNamecomponentName=newComponentName("com.sina.weibo", "com.sina.weibo.activity.JumpActivity");

//shareIntent.setComponent(componentName);

shareIntent.setPackage("com.sina.weibo");

//4開始分享

context.startActivity(Intent.createChooser(shareIntent, "分享圖片"));

? ? }

? ? /**

* 獲取制定包名應用的版本的versionCode

? ? *

* @paramcontext

* @param

* @return

? ? */

publicstaticintgetVersionCode(Contextcontext,StringpackageName) {

try{

PackageManagermanager=context.getPackageManager();

PackageInfoinfo=manager.getPackageInfo(packageName,0);

intversion=info.versionCode;

returnversion;

}catch(Exceptione) {

e.printStackTrace();

return0;

? ? ? ? }

? ? }

}


? ?使用時候? ? ?(切記 切記 切記 圖片必須是下載到本地的!k铩8敕邸)

?動態(tài)申請權限??

private static final int REQUEST_EXTERNAL_STORAGE = 1;

private static String[] PERMISSIONS_STORAGE = {

? ? ? ? Manifest.permission.READ_EXTERNAL_STORAGE,

? ? ? ? Manifest.permission.WRITE_EXTERNAL_STORAGE

};

//動態(tài)獲取內存存儲權限

public void verifyStoragePermissions(Activity activity) {

? ? int permission = ActivityCompat.checkSelfPermission(activity,

? ? ? ? ? ? Manifest.permission.WRITE_EXTERNAL_STORAGE);

? ? if (permission != PackageManager.PERMISSION_GRANTED) {

? ? ? ? if (!ActivityCompat.shouldShowRequestPermissionRationale(ShareDemoActivity.this,

? ? ? ? ? ? ? ? Manifest.permission.READ_EXTERNAL_STORAGE)) {

? ? ? ? ? ? ActivityCompat.requestPermissions(ShareDemoActivity.this,

? ? ? ? ? ? ? ? ? ? PERMISSIONS_STORAGE,

? ? ? ? ? ? ? ? ? ? REQUEST_EXTERNAL_STORAGE);

? ? ? ? }

? ? ? ? ActivityCompat.requestPermissions(ShareDemoActivity.this,

? ? ? ? ? ? ? ? PERMISSIONS_STORAGE,

? ? ? ? ? ? ? ? REQUEST_EXTERNAL_STORAGE);

? ? }

? ? while ((ContextCompat.checkSelfPermission(ShareDemoActivity.this,

? ? ? ? ? ? Manifest.permission.READ_EXTERNAL_STORAGE))!= PackageManager.PERMISSION_GRANTED) {

? ? }

}

@Override

public void onRequestPermissionsResult(int requestCode,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String permissions[], int[] grantResults) {

}


? ?完成!Wパ蕖4セ!!? ?


? CSDN地址:?https://blog.csdn.net/shuai497331206

?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末儡首,一起剝皮案震驚了整個濱河市片任,隨后出現的幾起案子,更是在濱河造成了極大的恐慌蔬胯,老刑警劉巖对供,帶你破解...
    沈念sama閱讀 216,997評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現場離奇詭異笔宿,居然都是意外死亡犁钟,警方通過查閱死者的電腦和手機,發(fā)現死者居然都...
    沈念sama閱讀 92,603評論 3 392
  • 文/潘曉璐 我一進店門泼橘,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人炬灭,你說我怎么就攤上這事∶自福” “怎么了?”我有些...
    開封第一講書人閱讀 163,359評論 0 353
  • 文/不壞的土叔 我叫張陵鼻吮,是天一觀的道長育苟。 經常有香客問我椎木,道長,這世上最難降的妖魔是什么香椎? 我笑而不...
    開封第一講書人閱讀 58,309評論 1 292
  • 正文 為了忘掉前任漱竖,我火速辦了婚禮,結果婚禮上畜伐,老公的妹妹穿的比我還像新娘。我一直安慰自己玛界,他們只是感情好,可當我...
    茶點故事閱讀 67,346評論 6 390
  • 文/花漫 我一把揭開白布良狈。 她就那樣靜靜地躺著鲤脏,像睡著了一般吕朵。 火紅的嫁衣襯著肌膚如雪窥突。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,258評論 1 300
  • 那天梧税,我揣著相機與錄音称近,去河邊找鬼。 笑死凳谦,一個胖子當著我的面吹牛衡未,可吹牛的內容都是我干的。 我是一名探鬼主播如失,決...
    沈念sama閱讀 40,122評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼送粱,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了脆丁?” 一聲冷哼從身側響起橄镜,我...
    開封第一講書人閱讀 38,970評論 0 275
  • 序言:老撾萬榮一對情侶失蹤洽胶,失蹤者是張志新(化名)和其女友劉穎裆馒,沒想到半個月后,有當地人在樹林里發(fā)現了一具尸體喷好,經...
    沈念sama閱讀 45,403評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡梗搅,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,596評論 3 334
  • 正文 我和宋清朗相戀三年效览,在試婚紗的時候發(fā)現自己被綠了荡短。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片掘托。...
    茶點故事閱讀 39,769評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖弯院,靈堂內的尸體忽然破棺而出泪掀,到底是詐尸還是另有隱情,我是刑警寧澤异赫,帶...
    沈念sama閱讀 35,464評論 5 344
  • 正文 年R本政府宣布祝辣,位于F島的核電站,受9級特大地震影響蝙斜,放射性物質發(fā)生泄漏孕荠。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,075評論 3 327
  • 文/蒙蒙 一弯予、第九天 我趴在偏房一處隱蔽的房頂上張望个曙。 院中可真熱鬧,春花似錦垦搬、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽馋艺。三九已至丈钙,卻和暖如春交汤,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背芙扎。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評論 1 269
  • 我被黑心中介騙來泰國打工戒洼, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人圈浇。 一個月前我還...
    沈念sama閱讀 47,831評論 2 370
  • 正文 我出身青樓磷蜀,卻偏偏與公主長得像,于是被迫代替她去往敵國和親褐隆。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,678評論 2 354