android7.0实现分享图片到朋友圈功能
在Android7.0中,系统对scheme为file://的uri进行了限制,所以通过这种uri来进行分享的一些接口就不能用了,比如使用代码来调用分享朋友圈的接口。
此时就得使用其他的URI scheme来代替 file://,比如MediaStore的 content://。直接上代码:
package com.example.mvpfour.activity;
import android.content.ComponentName;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import com.example.mvpfour.BaseApplication;
import com.example.mvpfour.R;
import com.example.mvpfour.utils.LogUtils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Created by agen on 2018/7/11.
*/
public class ShareActivity extends AppCompatActivity implements View.OnClickListener {
private Button share_image;
private Button share_url;
private Button share_weixin;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.share_activity);
initView();
}
private void initView() {
share_image = (Button) findViewById(R.id.share_image);
share_image.setOnClickListener(this);
share_url = (Button) findViewById(R.id.share_url);
share_url.setOnClickListener(this);
share_weixin = (Button) findViewById(R.id.share_weixin);
share_weixin.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.share_image:
shareImage();
break;
case R.id.share_url:
shareUrl();
break;
case R.id.share_weixin:
shareWeiXin();
break;
}
}
private void shareWeiXin() {
LogUtils.d("shareWeiXin");
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addCategory("android.intent.category.DEFAULT");
// 如果需要指定分享到某个app,配置 componentName 即可
// 分享精确到微信的页面,朋友圈页面,或者选择好友分享页面
ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
intent.setComponent(comp);
//这种方式不能分享文字
/*intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "f分享");
intent.putExtra(Intent.EXTRA_TEXT, "HI 推荐您使用一款软件:" + "http://www.baidu.com");
intent = Intent.createChooser(intent, "分享");
*/
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
Uri uriToImage = saveBitmap(bitmap, "share_bitmap");
intent.putExtra(Intent.EXTRA_STREAM, uriToImage);
// 指定发送内容的类型 (MIME type)
intent.setType("image/jpeg");
startActivity(Intent.createChooser(intent, "Share to..."));
// startActivity(intent);
}
private void shareUrl() {
Intent share_intent = new Intent();
share_intent.setAction(Intent.ACTION_SEND);
share_intent.setType("text/plain");
share_intent.putExtra(Intent.EXTRA_SUBJECT, "f分享");
share_intent.putExtra(Intent.EXTRA_TEXT, "HI 推荐您使用一款软件:" + "http://www.baidu.com");
share_intent = Intent.createChooser(share_intent, "分享");
startActivity(share_intent);
}
private void shareImage() {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
// 比如发送二进制文件数据流内容(比如图片、视频、音频文件等等)
// 指定发送的内容 (EXTRA_STREAM 对于文件 Uri )
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
Uri uriToImage = saveBitmap(bitmap, "share_bitmap");
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
// 指定发送内容的类型 (MIME type)
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, "Share to..."));
}
/**
* 将图片存到本地
*/
private static Uri saveBitmap(Bitmap bm, String picName) {
try {
String dir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/renji/" + picName + ".jpg";
File f = new File(dir);
if (!f.exists()) {
f.getParentFile().mkdirs();
f.createNewFile();
}
FileOutputStream out = new FileOutputStream(f);
LogUtils.d("bm=" + bm);
bm.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
Uri uri = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//uri= FileProvider.getUriForFile(BaseApplication.getAppContext(), "com.example.mvpfour.fileprovider", f);
//修复微信在7.0崩溃的问题
uri = Uri.parse(android.provider.MediaStore.Images.Media.insertImage(
BaseApplication.getAppContext().getContentResolver(), f.getAbsolutePath(),
picName + ".jpg", null));
} else {
uri = Uri.fromFile(f);
}
return uri;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}