本文實(shí)例為大家分享了android實(shí)現(xiàn)分享圖片到朋友圈功能的具體代碼,供大家參考,具體內(nèi)容如下
在Android7.0中,系統(tǒng)對(duì)scheme為file://的uri進(jìn)行了限制,所以通過(guò)這種uri來(lái)進(jìn)行分享的一些接口就不能用了,比如使用代碼來(lái)調(diào)用分享朋友圈的接口。
此時(shí)就得使用其他的URI scheme來(lái)代替 file://,比如MediaStore的 content://。直接上代碼:
private static boolean checkInstallation(Context context, String packageName) { try { context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES); return true; } catch (PackageManager.NameNotFoundException e) { return false; } } public static void shareToWeChat(View view, Context context) { // TODO: 2015/12/13 將需要分享到微信的圖片準(zhǔn)備好 try { if (!checkInstallation(context, "com.tencent.mm")) { SnackBarUtil.show(view, R.string.share_no_wechat); return; } Intent intent = new Intent(); //分享精確到微信的頁(yè)面,朋友圈頁(yè)面,或者選擇好友分享頁(yè)面 ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI"); intent.setComponent(comp); intent.setAction(Intent.ACTION_SEND_MULTIPLE); intent.setType("image/*"); // intent.setType("text/plain"); //添加Uri圖片地址 // String msg=String.format(getString(R.string.share_content), getString(R.string.app_name), getLatestWeekStatistics() + ""); String msg = context.getString(R.string.share_content); intent.putExtra("Kdescription", msg); ArrayListimageUris = new ArrayList (); // TODO: 2016/3/8 根據(jù)不同圖片來(lái)設(shè)置分享 File dir = context.getExternalFilesDir(null); if (dir == null || dir.getAbsolutePath().equals("")) { dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath()); } File pic = new File(dir, "bigbang.jpg"); pic.deleteOnExit(); BitmapDrawable bitmapDrawable; if (Build.VERSION.SDK_INT < 22) { bitmapDrawable = (BitmapDrawable) context.getResources().getDrawable(R.mipmap.bannar); } else { bitmapDrawable = (BitmapDrawable) context.getDrawable(R.mipmap.bannar); } try { bitmapDrawable.getBitmap().compress(Bitmap.CompressFormat.JPEG, 75, new FileOutputStream(pic)); } catch (FileNotFoundException e) { e.printStackTrace(); } if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { imageUris.add(Uri.fromFile(pic)); }else { //修復(fù)微信在7.0崩潰的問(wèn)題 Uri uri =Uri.parse(android.provider.MediaStore.Images.Media.insertImage(context.getContentResolver(), pic.getAbsolutePath(), "bigbang.jpg", null)); imageUris.add(uri); } intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris); ((Activity) context).startActivityForResult(intent, 1000); }catch (Throwable e){ SnackBarUtil.show(view,R.string.share_error); }