android7.0实现分享图片到朋友圈功能

发布时间:2020-08-29 00:46:50

来源:脚本之家

阅读:132

作者:十个雨点

本文实例为大家分享了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 imageUris = new ArrayList();

// 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

...

android:name="android.support.v4.content.FileProvider"

android:authorities="com.example.android.fileprovider"

android:exported="false"

android:grantUriPermissions="true">

android:name="android.support.FILE_PROVIDER_PATHS"

android:resource="@xml/file_paths">

...

在res/xml/文件夹下新建文件file_paths.xml:

参考:stackoverflow

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

android 做分享到朋友圈,android7.0实现分享图片到朋友圈功能相关推荐

  1. Android中获取系统语言(适用于Android7.0以上系统)

    2019独角兽企业重金招聘Python工程师标准>>> public static String getSysLang(Context context) {Locale locale ...

  2. android7.0 拍照以及图片剪辑--(再次修改)

    如果你是在fragment里头调用的拍照,出了bug 可以查看 这篇文章 点我 不懂的可以留言. 这篇博客对于7.0的版本的修改.主要是在文件存储的处理. 6.0.8.0版本使用本文的存储文件会出现 ...

  3. android Telephony学习 --- 第七篇 android7.0 来电(MT)流程

    我们先看下7.0来电大体流程: Framework modem接收到来电通知消息后,以AT指令的方式上报RIL层,RIL层通过sokcet将消息发送给RILJ, 上报事件ID: RIL_UNSOL_R ...

  4. 乐视android版本点四下,EUI5.9+Android7.0刷机包

    乐视EUI5.9系统刷机包,乐视更新了EUI5.9,不仅有语音助手还有iPhone的小白点悬浮球,功能齐全,界面简单,实在是应该给程序员点个赞! 乐视EUI5.9系统刷机包更新内容: 设置 新增悬浮球 ...

  5. Android7.0相机图册图片剪切封装 - ucrop框架

    说明: * 两天使用 ucrop框架 实现了从相册和图册选择图片,然后剪切返回显示的功能.同时兼容了相机拍照7.0. * 对相机调用.相册调用.图片剪切封装成了工具类. * 剪切图片框架ucrop我也 ...

  6. android点击按钮弹出图片,用android做的一个简单的点击按钮显示图片的程序

    其实,在这之前我已经做了一个点击按钮的小程序,只不过它只是用来在界面上显示一些文字或者是用一个对话框来显示内容.按理说,做显示图片应该是不会有太大的问题了,可是问题还是来了.在我把这些个问题解决之后, ...

  7. Android7.0适配方案

    1安装时解析错误 我们的App通常会有检查更新的功能.用户在收到提示更新并且下载完后,会自动打开安装页面让用户来去安装.这时就会出现安装错误的问题,这类的问题的可能性比较多.比如较低版本的App想要覆 ...

  8. android 版本更新和下载安装适配7.0 8.0 9.0

    在android 7.0之前版本更新其实相当简单,只需要使用系统下载器就能够完成下载之后安装,但是在7.0之后android升级安全机制,下载安装受到一些限制.工具类拿去用 public class ...

  9. Android7.0调用系统相机拍照、访问相册问题。

    关于Android7.0调用系统相机拍照.访问相册的一些问题: 在Android6.0中Google提出了动态申请权限的Api,调用相机拍照,访问SDcard等操作都需要先申请对应的权限如下: < ...

最新文章

  1. cmake语法【一】
  2. Linux内核学习总结
  3. Flex很可能会消失
  4. HDU 4619 Warm up 2 最大独立集
  5. C#全局键盘监听(Hook)的使用(转载)
  6. Winform中设置ZedGraph的曲线为散点图
  7. mockito java_Java:使用Mockito模拟ResultSet
  8. 1进程 ppid_杀死僵尸进程,你需要这些神奇的Linux命令行
  9. 一文看懂什么是MQ消息队列
  10. ecshop 后台 审核功能
  11. qt信号和槽与对话框
  12. linux安全擦除ssd命令,如何在不破坏SSD的情况下安全擦除SSD | MOS86
  13. 华为笔试题:根据子网掩码判断两个IP地址是否在同一子网,并输出IP1的网络号
  14. 用户权限管理模块的数据库设计
  15. 思维导图怎么画简单又漂亮?脑图轻松制作方法
  16. 古筝d调变降e调怎么办_为什么古筝总要调音、还总调不好?
  17. 刘宇凡:坚持就是写文章的最大技巧
  18. deepmind dnc 相关的软件下载资源
  19. 计算机新闻学phd,纽约州立大学石溪分校计算机博士PHD全奖录取
  20. 什么是“蓝牙距离感应装置”

热门文章

  1. ipad如何与计算机连接网络连接不上,ipad连不上电脑怎么办 ipad无法连接电脑的解决步骤介绍...
  2. 力控设置登陆密码的详细操作步骤
  3. Unity A星(A Star/A*)寻路算法
  4. 洛谷 P2320 [HNOI2006]鬼谷子的钱袋 思维+二进制
  5. 【医疗健康项目】传智健康项目(六)
  6. 电魂网络2018年营收达4.48亿元 同比下降10.19%
  7. 香港银行和科技公司竞争已经打响
  8. 关于安装Microsoft Office 2016专业版出现0x80131049回滚错误的解决方案
  9. Redis:Jedis连接池JedisPool
  10. 用python画路飞代码_用Python画路飞骷髅图案【附代码】