一,注册并申请AppKey
注册开发者帐号
二,添加依赖jar包
创建并配置工程
三,分享消息
分享消息到QQ(定向分享)
四,分享mipmap下的图片
单纯分享一张图片到QQ,因为QQ只支持本地图片,但是我想分享的图片在mipmap文件夹下,所以先将图片保存在本地,再分享
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import com.tencent.connect.share.QQShare;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class ShareActivity extends AppCompatActivity implements View.OnLongClickListener{
public static final String TAG = "Chunna=ShareActivity";
private ImageView ivQRCode;
Tencent mTencent;
private String APP_ID_QQ = "222222";//QQ AppId
BaseUiListener listener;
final int REQUEST_WRITE=1;//申请权限的请求码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share);
initView();
Log.d(TAG,"开始载入第三方");
initTecent();
}
private void initTecent() {
// Tencent类是SDK的主要实现类,开发者可通过Tencent类访问腾讯开放的OpenAPI。
// 其中APP_ID是分配给第三方应用的appid,类型为String。
mTencent = Tencent.createInstance(APP_ID_QQ,this);
// 1.4版本:此处需新增参数,传入应用程序的全局context,可通过activity的getApplicationContext方法获取
listener = new BaseUiListener();
}
private void initView() {
ivQRCode = (ImageView)findViewById(R.id.iv_share_QRcode);
ivQRCode.setOnLongClickListener(this);
}
@Override
public boolean onLongClick(View view) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_share_qrcode);
//6.0以上的版本需要动态申请写入SD卡权限
hasPermission(bitmap);
//qqShare();
return true;
}
//qq分享应用
public void qqShare() {
Bundle params = new Bundle();
params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
params.putString(QQShare.SHARE_TO_QQ_TITLE, "我的应用");
params.putString(QQShare.SHARE_TO_QQ_SUMMARY,"我的应用说明");
params.putString(QQShare.SHARE_TO_QQ_TARGET_URL,"http://blog.csdn.net/zcn596785154");
params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL,"http://avatar.csdn.net/C/3/D/1_u013451048.jpg");
params.putString(QQShare.SHARE_TO_QQ_APP_NAME, "应用名");
mTencent.shareToQQ(this, params, listener);
}
//QQ分享图片
private void onClickShare(String path) {
Log.d(TAG,"22文件路径为:"+path);
Bundle params = new Bundle();
params.putString(QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL,path);
params.putString(QQShare.SHARE_TO_QQ_APP_NAME, "应用名");
params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_IMAGE);
params.putInt(QQShare.SHARE_TO_QQ_EXT_INT, QQShare.SHARE_TO_QQ_FLAG_QZONE_ITEM_HIDE);
mTencent.shareToQQ(this, params, listener);
}
/**
* 保存图片到本地
* */
public void saveBitmap(Bitmap bm) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_share_qrcode);
File file ;
isHaveSDCard();
if (isHaveSDCard()) {
file = Environment.getExternalStorageDirectory();
} else {
file = Environment.getDataDirectory();
}
file = new File(file.getPath() + "/MotieReader/data/");
if (!file.isDirectory()) {
file.delete();
file.mkdirs();
}
if (!file.exists()) {
file.mkdirs();
}
Log.d(TAG,"文件路径为:"+file.getPath());
Log.d(TAG,"文件名称为:"+file.getName());
writeBitmap(file.getPath(), "qrcode.png", bm);
}
/**
* 将要分享的图片先保存在本地
* @param path
* @param name
* @param bitmap
*/
public void writeBitmap(String path, String name, Bitmap bitmap) {
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
File _file = new File(path +"/"+ name);
if (_file.exists()) {
_file.delete();
}
createFile(_file.getPath());
FileOutputStream fos = null;
try {
fos = new FileOutputStream(_file);
if (name != null && !"".equals(name)) {
int index = name.lastIndexOf(".");
if (index != -1 && (index + 1) < name.length()) {
String extension = name.substring(index + 1).toLowerCase();
if ("png".equals(extension)) {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
} else if ("jpg".equals(extension)
|| "jpeg".equals(extension)) {
bitmap.compress(Bitmap.CompressFormat.JPEG, 75, fos);
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//文件保存后,调用分享
onClickShare(_file.getPath());
}
//判断授权的方法 授权成功直接调用写入方法 这是监听的回调
//参数 上下文 授权结果的数组 申请授权的数组
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode==REQUEST_WRITE&&grantResults[0]== PackageManager.PERMISSION_GRANTED){
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_share_qrcode);
saveBitmap(bitmap);
}
}
//如果文件不存在,则创建文件及目录
public boolean createFile(String destFileName) {
Log.d(TAG,"创建文件 "+destFileName);
File file = new File(destFileName);
if(file.exists()) {
Log.d(TAG,"创建单个文件" + destFileName + "失败,目标文件已存在!");
return false;
}
if (destFileName.endsWith(File.separator)) {
Log.d(TAG,"创建单个文件" + destFileName + "失败,目标文件不能为目录!");
return false;
}
//判断目标文件所在的目录是否存在
if(!file.getParentFile().exists()) {
//如果目标文件所在的目录不存在,则创建父目录
Log.d(TAG,"目标文件所在目录不存在,准备创建它!");
if(!file.getParentFile().mkdirs()) {
Log.d(TAG,"创建目标文件所在目录失败!");
return false;
}
}
//创建目标文件
try {
if (file.createNewFile()) {
Log.d(TAG,"创建单个文件" + destFileName + "成功!");
return true;
} else {
Log.d(TAG,"创建单个文件" + destFileName + "失败!");
return false;
}
} catch (IOException e) {
e.printStackTrace();
Log.d(TAG,"创建单个文件" + destFileName + "失败!" + e.getMessage());
return false;
}
}
public boolean isHaveSDCard() {
String SDState = android.os.Environment.getExternalStorageState();
if (SDState.equals(android.os.Environment.MEDIA_MOUNTED)) {
return true;
}
return false;
}
public void hasPermission(Bitmap bitmap){
//判断是否6.0以上的手机 不是就不用
if(Build.VERSION.SDK_INT>=23){
//判断是否有这个权限
if(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
//2、申请权限: 参数二:权限的数组;参数三:请求码
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUEST_WRITE);
}else {
saveBitmap(bitmap);
}
} else{
saveBitmap(bitmap);
}
}
private class BaseUiListener implements IUiListener {
private static final String TAG = "Chunna==BaseUiListener";
@Override
public void onComplete(Object o) {
//分享成功后回调
Log.d(TAG,"分享成功!");
}
@Override
public void onError(UiError uiError) {
//分享失败后回调
Log.d(TAG,"分享失败!");
}
@Override
public void onCancel() {
//取消分享后回调
Log.d(TAG,"分享取消!");
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (null != mTencent)
mTencent.onActivityResult(requestCode, resultCode, data);
}
}