本文实例为大家分享了android实现分享图片到朋友圈功能的具体代码,供大家参考,具体内容如下
在Android7.0中,系统对scheme为file://的uri进行了限制,所以通过这种uri来进行分享的一些接口就不能用了,比如使用代码来调用分享朋友圈的接口。
此时就得使用其他的URI scheme来代替 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 将需要分享到微信的图片准备好 try { if (!checkInstallation(context, "com.tencent.mm")) { SnackBarUtil.show(view, R.string.share_no_wechat); return; } Intent intent = new Intent(); //分享精确到微信的页面,朋友圈页面,或者选择好友分享页面 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); ArrayList<Uri> imageUris = new ArrayList<Uri>(); // TODO: 2016/3/8 根据不同图片来设置分享 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 { //修复微信在7.0崩溃的问题 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); }
还有一种方式,就是FileProvider来分享文件,操作起来稍微复杂一点,大概代码如下(代码功能是拍照的):
String mCurrentPhotoPath; private File createImageFile() throws IOException { // Create an image file name String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String imageFileName = "JPEG_" + timeStamp + "_"; File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); File image = File.createTempFile( imageFileName, /* prefix */ ".jpg", /* suffix */ storageDir /* directory */ ); // Save a file: path for use with ACTION_VIEW intents mCurrentPhotoPath = "file:" + image.getAbsolutePath(); return image; } static final int REQUEST_TAKE_PHOTO = 1; private void dispatchTakePictureIntent() { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // Ensure that there's a camera activity to handle the intent if (takePictureIntent.resolveActivity(getPackageManager()) != null) { // Create the File where the photo should go File photoFile = null; try { photoFile = createImageFile(); } catch (IOException ex) { // Error occurred while creating the File ... } // Continue only if the File was successfully created if (photoFile != null) { Uri photoURI = FileProvider.getUriForFile(this, "com.example.android.fileprovider", photoFile); takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO); } } }
还要在manifest中声明这个FileProvider
<application> ... <provider android:name="android.support.v4.content.FileProvider" android:authorities="com.example.android.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"></meta-data> </provider> ... </application>
在res/xml/文件夹下新建文件file_paths.xml:
<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="my_images" path="Android/data/com.example.package.name/files/Pictures" /> </paths>
参考:stackoverflow
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。
本文向大家介绍android实现多图文分享朋友圈功能,包括了android实现多图文分享朋友圈功能的使用技巧和注意事项,需要的朋友参考一下 很多安卓程序员都在寻找如何调用系统分享可以实现朋友圈多图加文字分享的功能,小编经过测试入坑后,为你整理以下内容: 其中最关键的就是: 文字部分一直分享失败,搞了很久都分享失败后来才发现是需要加上这一句了·····坑! 原来Kdescription是微信描述信息
本文向大家介绍Javascript 实现微信分享(QQ、朋友圈、分享给朋友),包括了Javascript 实现微信分享(QQ、朋友圈、分享给朋友)的使用技巧和注意事项,需要的朋友参考一下 最近做微信开发,对微信公众号的开发,现在好的都是分享到朋友圈,QQ,分享给好友等分享功能,这里记录下,有需要的朋友也可以看下。 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
本文向大家介绍微信小程序通过保存图片分享到朋友圈功能,包括了微信小程序通过保存图片分享到朋友圈功能的使用技巧和注意事项,需要的朋友参考一下 说明 首先说明一点,小程序内是不能直接分享到朋友圈的。所以只能通过生成图片,携带小程序二维码,保存到手机相册,让用户自己选择发到朋友圈。然后可以通过在小程序中识别二维码来进入小程序的指定页面。参考市面上支持分享的应用,基本都是这种实现方式。 准备阶段 1.通过
本文向大家介绍Python实现九宫格式的朋友圈功能内附“马云”朋友圈,包括了Python实现九宫格式的朋友圈功能内附“马云”朋友圈的使用技巧和注意事项,需要的朋友参考一下 PIL(Python Imaging Library)是一个非常强大的Python库,但是它支持Python2.X, 在Python3中则使用的是Pillow库,它是从PIL中fork出来的一个分支。提供了非常强大的图片处理能力
本文向大家介绍Android PraiseTextView实现朋友圈点赞功能,包括了Android PraiseTextView实现朋友圈点赞功能的使用技巧和注意事项,需要的朋友参考一下 PraiseTextView 说明 我是将朋友圈分成了几个独立模块单独自定义的View,通过回调完成交互,耦合性算是非常低了,主要有以下及部分: 1.评论布局(自定义TextView) CommentListTe
本文向大家介绍iOS实现微信朋友圈与摇一摇功能,包括了iOS实现微信朋友圈与摇一摇功能的使用技巧和注意事项,需要的朋友参考一下 本Demo为练手小项目,主要是熟悉目前主流APP的架构模式.此项目中采用MVC设计模式,纯代码和少许XIB方式实现.主要实现了朋友圈功能和摇一摇功能. 预览效果: 主要重点 1.整体架构 利用UITabBarController和UINavigationControlle