亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

解決Android調用系統分享給微信,出現分享失敗,分享多文件必須為圖片格式的問題

瀏覽:104日期:2022-06-06 15:01:25

解決Android調用系統分享圖片給微信,出現分享失敗,分享多文件必須為圖片格式

近期應公司需求,分享多圖片到微信的功能,之前一直是用微信自己家SDK實現分享,但是查看微信的原生SDK是不具備多圖分享的。在網上查找解決辦法,直接調用手機系統進行分享,進行系統分享時分享給QQ,微博等都可以,但分享微信時就會出現分享失敗,分享多文件必須為圖片格式,看網上各路大神都各顯神通都沒解決具體問題,于是自己就總結出此篇文章為后來者少踩些坑,讓你更快完成公司交給你的任務,讓產品經理對你刮目相看,話不多說直接上干貨。

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');//調用系統分享給微信朋友 comp1 = new ComponentName('com.tencent.mm', 'com.tencent.mm.ui.tools.ShareToTimeLineUI');//調用系統分享給微信朋友圈 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);//這個是版本大于Android7.0(包含)臨時訪問文件,沒有這個會報異常 } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } ArrayList<Uri> imageUris = new ArrayList<Uri>(); imageUris.add(UrigetImageContentUri(NativePhoto.this,new File(photoPath)));//這個是分享本地存儲的圖片 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('*');系統會調出所有的分享軟件 shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris); shareIntent.setType('image/*'); startActivity(shareIntent); }

//如果是微信分享的話一定一定將這個直接復制到自己項目中,將自己圖片路徑換為content:不然就會出現上述錯誤

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; }

這樣就解決了調用系統分享報出分享失敗,分享多文件必須為圖片格式的錯誤了。

在AndroidManifest中臨時文件注冊解決Android7.0版本及其之后文件uri報錯問題

<application android:allowBackup='true' android:icon='@drawable/logo' android:label='@string/app_name1' android:supportsRtl='true' android:theme='@style/AppTheme'> <!-- //臨時訪問文件的注冊--> <provider android:name='android.support.v4.content.FileProvider' android:authorities='com.lipuwulian.blesample.provider'//這是自己的包名加“.provider” android:exported='false' android:grantUriPermissions='true'> <meta-dataandroid: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文件夾下創建xml文件夾、

解決Android調用系統分享給微信,出現分享失敗,分享多文件必須為圖片格式的問題

file_paths文件里的內容: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>

到這里就結束了,希望能夠幫到大家哦!IT需要愛與和平😊

到此這篇關于解決Android調用系統分享給微信,出現分享失敗,分享多文件必須為圖片格式的問題的文章就介紹到這了,更多相關android 調用系統分享給微信內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: 微信
主站蜘蛛池模板: 欧美一区二区在线免费观看 | 真实男女xx00动态视频免费 | 一级黄色欧美片 | 国产精品久久二区三区色裕 | 欧美区在线 | 一级毛片免费观看不卡视频 | 国产一区免费视频 | 在线xxxx| 精品特级一级毛片免费观看 | 啪啪自拍视频 | 伊人精品影院 | 亚洲欧洲日产专区 | 久久综合一区二区三区 | 日本一卡2卡三卡4卡 免费网站仙踪 | 国产一二三区精品 | www.一级毛片 | japanese护士日本xx丝袜 | 毛茸茸年轻成熟亚洲人 | 国产精品vv在线 | 污网站网址 | 亚洲精品第一国产综合高清 | 国产一级内谢a级高清毛片 国产一级毛片大陆 | 调教~奴●メイドの馆 | 国产三级精品三级在线观看 | 91系列在线 | 欧美一级片网 | 欧美大尺码毛片 | 26uuu老色哥| 综合在线视频 | 精品国产毛片 | 男女做污污无遮挡激烈免费 | 成熟女性毛茸茸撒尿厕所 | 欧美伦理一区二区三区 | 久久草影视 | 国产51社区精品视频资源 | 欧美视频国产 | 免费视频一区二区 | 黄色福利片 | 国产孕妇孕交600集 国产在视频线精品视频www666 | 国产xxwwxxww视频 | 韩日视频在线观看 |