常见功能Mark:QQ分享官方第三方接入。

一、创建并配置工程 参考腾讯官方文档:

点击打开链接

简单抄过来一些:

1、创建一个工程,并把open-sdk.jar文件和mta_sdk_x.x.x.jar文件拷贝到libs(或lib)目录下

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

2. 配置AndroidManifest

在应用的AndroidManifest.xml增加配置的节点下增加以下配置(注:不配置将会导致无法调用API);(下面是我自己的manifest代码,直接粘贴,标红的地方为AppKey)

package="com.example.qqsharedm"

android:versionCode="1"

android:versionName="1.0" >

android:minSdkVersion="8"

android:targetSdkVersion="21" />

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name" >

android:name="com.tencent.tauth.AuthActivity"

android:launchMode="singleTask"

android:noHistory="true" >

android:name="com.tencent.connect.common.AssistActivity"

android:configChanges="orientation|keyboardHidden|screenSize"

android:theme="@android:style/Theme.Translucent.NoTitleBar" />

android:name=".MainActivity"

android:label="@string/app_name" >

二、正文配置QQ分享Demo: 一)实例化:

private Tencent

mTencent;

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initViews();

// Tencent类是SDK的主要实现类,开发者可通过Tencent类访问腾讯开放的OpenAPI。

// 其中APP_ID是分配给第三方应用的appid,类型为String。

mTencent = Tencent.createInstance("222222",

this.getApplicationContext());

// 1.4版本:此处需新增参数,传入应用程序的全局context,可通过activity的getApplicationContext方法获取

bt_1.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Bitmap b;

if (tv_1.getDrawingCache() != null) {

b = tv_1.getDrawingCache();

} else {

b = loadBitmapFromView(tv_1);

}

saveBitmap(b);

onClickShareToQQ();

Toast.makeText(getApplicationContext(), "保存成功",

Toast.LENGTH_SHORT).show();

}

});

}二、分享消息代码:(我做的是分享纯图片):这个比较恶心的一点是图片必须取路径,要么网络路径要么本地路径。让人比较无奈。(具体路径及保存图片代码见最后注释)

private void onClickShareToQQ() {

Bundle shareParams = new Bundle();

shareParams.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE,

QQShare.SHARE_TO_QQ_TYPE_IMAGE);

shareParams.putString(QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL,

File.getPath() + picName);

shareParams.putString(QQShare.SHARE_TO_QQ_APP_NAME, "LPS CRM");

shareParams.putInt(QQShare.SHARE_TO_QQ_EXT_INT,

QQShare.SHARE_TO_QQ_FLAG_QZONE_ITEM_HIDE);

doShareToQQ(shareParams);

}三、分享后实现回调

private void doShareToQQ(Bundle params) {

mTencent.shareToQQ(MainActivity.this, params, new BaseUiListener() {

protected void doComplete(JSONObject values) {

Toast.makeText(getApplicationContext(), "成功",

Toast.LENGTH_SHORT);

}

@Override

public void onError(UiError e) {

}

@Override

public void onCancel() {

}

});

}两端代码均为回调,其实自定义与否看需求。

private class BaseUiListener implements IUiListener {

@Override

public void onCancel() {

// TODO Auto-generated method stub

}

@Override

public void onComplete(Object arg0) {

// TODO Auto-generated method stub

}

@Override

public void onError(UiError arg0) {

// TODO Auto-generated method stub

}

}

注释:

核心代码:抓取view图片缓存保存bitmap判空代码:

public static Bitmap loadBitmapFromView(View v) {

if (v == null) {

return null;

}

v.setDrawingCacheEnabled(true);

Bitmap screenshot;

v.measure(MeasureSpec.makeMeasureSpec(v.getWidth(),

MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(

v.getHeight(), MeasureSpec.UNSPECIFIED));

v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());

v.buildDrawingCache();

screenshot = v.getDrawingCache();

if (screenshot == null) {

v.setDrawingCacheEnabled(true);

screenshot = Bitmap.createBitmap(v.getWidth(), v.getHeight(),

Bitmap.Config.ARGB_8888);

Canvas c = new Canvas(screenshot);

c.translate(-v.getScrollX(), -v.getScrollY());

v.draw(c);

return screenshot;

}

return screenshot;

}

1)保存图片到本地

/**

* 保存图片到本地

* */

public void saveBitmap(Bitmap bm) {

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();

}

writeBitmap(File.getPath(), picName, bm);

}

2)保存图片到本地

/**

* 保存图片

*

* @param path

* @param name

* @param bitmap

*/

public static 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();

}

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();

}

}

}

}三、是否有sd卡

public boolean isHaveSDCard() {

String SDState = android.os.Environment.getExternalStorageState();

if (SDState.equals(android.os.Environment.MEDIA_MOUNTED)) {

return true;

}

return false;

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐