解決Android調(diào)用系統(tǒng)分享給微信,出現(xiàn)分享失敗,分享多文件必須為圖片格式的問題
解決Android調(diào)用系統(tǒng)分享圖片給微信,出現(xiàn)分享失敗,分享多文件必須為圖片格式
近期應(yīng)公司需求,分享多圖片到微信的功能,之前一直是用微信自己家SDK實(shí)現(xiàn)分享,但是查看微信的原生SDK是不具備多圖分享的。在網(wǎng)上查找解決辦法,直接調(diào)用手機(jī)系統(tǒng)進(jìn)行分享,進(jìn)行系統(tǒng)分享時(shí)分享給QQ,微博等都可以,但分享微信時(shí)就會(huì)出現(xiàn)分享失敗,分享多文件必須為圖片格式,看網(wǎng)上各路大神都各顯神通都沒解決具體問題,于是自己就總結(jié)出此篇文章為后來者少踩些坑,讓你更快完成公司交給你的任務(wù),讓產(chǎn)品經(jīng)理對(duì)你刮目相看,話不多說直接上干貨。
private void systemShareWeChat(int shareTag,String photoPath){
Resources res=getResources();
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.share);
File f = null;
ComponentName comp1,comp;
comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");//調(diào)用系統(tǒng)分享給微信朋友
comp1 = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");//調(diào)用系統(tǒng)分享給微信朋友圈
try {
//將Android中drawable圖片保存到本地
String dir= Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"share"+".jpg";
f = new File(dir);
if (!f.exists()) {
f.getParentFile().mkdirs();
f.createNewFile();
}
FileOutputStream out = new FileOutputStream(f);
bmp.compress(Bitmap.CompressFormat.PNG, 80, out);
out.flush();
out.close();
Uri uri = FileProvider.getUriForFile(NativePhoto.this.getApplicationContext(),
"com.lipuwulian.blesample.provider", f);//這個(gè)是版本大于Android7.0(包含)臨時(shí)訪問文件,沒有這個(gè)會(huì)報(bào)異常
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace(); }
ArrayList<Uri> imageUris = new ArrayList<Uri>();
imageUris.add(UrigetImageContentUri(NativePhoto.this,new File(photoPath)));//這個(gè)是分享本地存儲(chǔ)的圖片
imageUris.add(UrigetImageContentUri(NativePhoto.this,f));
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
if(shareTag==0){
shareIntent.setComponent(comp1);//分享給微信朋友圈
}else if(shareTag==1){
shareIntent.setComponent(comp);//分享給微信朋友
}
//如果去掉shareIntent.setComponent("*");系統(tǒng)會(huì)調(diào)出所有的分享軟件
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
shareIntent.setType("image/*");
startActivity(shareIntent);
}
//如果是微信分享的話一定一定將這個(gè)直接復(fù)制到自己項(xiàng)目中,將自己圖片路徑換為content:不然就會(huì)出現(xiàn)上述錯(cuò)誤
public static Uri UrigetImageContentUri(Context context, File imageFile) {
String filePath = imageFile.getAbsolutePath();
Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Images.Media._ID}, MediaStore.Images.Media.DATA +"=? ", new String[]{filePath}, null);
Uri uri =null;
if (cursor !=null) {
if (cursor.moveToFirst()) {
int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
Uri baseUri = Uri.parse("content://media/external/images/media");
uri = Uri.withAppendedPath(baseUri, "" + id);
}
cursor.close();
}
if (uri ==null) {
ContentValues values =new ContentValues();
values.put(MediaStore.Images.Media.DATA, filePath);
uri = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
}
return uri;
}
這樣就解決了調(diào)用系統(tǒng)分享報(bào)出分享失敗,分享多文件必須為圖片格式的錯(cuò)誤了。
在AndroidManifest中臨時(shí)文件注冊(cè)解決Android7.0版本及其之后文件uri報(bào)錯(cuò)問題
<application
android:allowBackup="true"
android:icon="@drawable/logo"
android:label="@string/app_name1"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!-- //臨時(shí)訪問文件的注冊(cè)-->
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.lipuwulian.blesample.provider"http://這是自己的包名加“.provider”
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
<activity android:name="com.lipuwulian.blesample.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
在res文件夾下創(chuàng)建xml文件夾、

file_paths文件里的內(nèi)容:path是data/包名加.testapplication/
<?xml version="1.0" encoding="utf-8"?>
<resources>
<paths>
<external-path
name="files_root"
path="Android/data/com.lipuwulian.blesample.testapplication/" />
<external-path
name="external_storage_root"
path="." />
</paths>
</resources>
到這里就結(jié)束了,希望能夠幫到大家哦!IT需要愛與和平😊
到此這篇關(guān)于解決Android調(diào)用系統(tǒng)分享給微信,出現(xiàn)分享失敗,分享多文件必須為圖片格式的問題的文章就介紹到這了,更多相關(guān)android 調(diào)用系統(tǒng)分享給微信內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Adapter模式實(shí)戰(zhàn)之重構(gòu)鴻洋集團(tuán)的Android圓形菜單建行
這篇文章主要介紹了Adapter模式實(shí)戰(zhàn)之重構(gòu)鴻洋集團(tuán)的Android圓形菜單建行的相關(guān)資料,需要的朋友可以參考下2016-03-03
Android輔助功能AccessibilityService與搶紅包輔助
搶紅包的原理都差不多,一般是用Android的輔助功能(AccessibilityService類)先監(jiān)聽通知欄事件或窗口變化事件來查找紅包關(guān)鍵字然后去模擬點(diǎn)擊或打開紅包2016-02-02
Android開發(fā)之?dāng)?shù)據(jù)的存儲(chǔ)方式詳解
本篇文章主要介紹了Android開發(fā)之?dāng)?shù)據(jù)的存儲(chǔ)方式,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧。2016-11-11
Android 開發(fā)使用Activity實(shí)現(xiàn)加載等待界面功能示例
這篇文章主要介紹了Android 開發(fā)使用Activity實(shí)現(xiàn)加載等待界面功能,結(jié)合實(shí)例形式詳細(xì)分析了Android基于Activity實(shí)現(xiàn)加載等待界面布局與功能操作技巧,需要的朋友可以參考下2020-05-05
Android實(shí)現(xiàn)觸發(fā)html頁面的Button控件點(diǎn)擊事件方式
這篇文章主要介紹了Android實(shí)現(xiàn)觸發(fā)html頁面的Button控件點(diǎn)擊事件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
詳解Android中Handler的實(shí)現(xiàn)原理
這篇文章主要為大家詳細(xì)介紹了Android中Handler的實(shí)現(xiàn)原理,本文深入分析 Android 的消息處理機(jī)制,了解 Handler 的工作原理,感興趣的小伙伴們可以參考一下2016-04-04

