安卓分享之QQ纯大图分享好友和空间

之前各种分享都做过,唯独QQ的不曾接入,可能我做的产品只是合适年龄大些的吧.扯远了。。。

刚开始直接去QQ互联开放平台文档中心添加jar包,初始化什么的就简单完成了,一运行坑点很多,下面就记录下

坑1.QQ分享单张图只能从本地获取路径分享,直接贴代码,便于大家搬运哈哈

//iv_img 是我把view转成图片的imgview,这里随意历史任何布局都可以转成你想要的图
Bitmap b;
if (iv_img.getDrawingCache() != null) {
    b = iv_img.getDrawingCache();
} else {
    b = loadBitmapFromView(iv_img);
}
saveBitmap(b);
onClickShareToQQ();
//view转成图片
public static Bitmap loadBitmapFromView(View v) {
    if (v == null) {
        return null;
    }
    v.setDrawingCacheEnabled(true);
    Bitmap screenshot;
    v.measure(View.MeasureSpec.makeMeasureSpec(v.getWidth(),
            View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(
            v.getHeight(), View.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;
}
/**
 * 保存图片到本地
 * */
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);

}
/**
 * 保存图片
 *
 * @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();
            }
        }
    }
}
//判断内存卡是否存在
public boolean isHaveSDCard() {
    String SDState = android.os.Environment.getExternalStorageState();
    if (SDState.equals(android.os.Environment.MEDIA_MOUNTED)) {
        return true;
    }
    return false;
}

//QQ分享好友和空间的代码

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");
        //分享到QQ好友 SHARE_TO_QQ_FLAG_QZONE_ITEM_HIDE 分享到空间SHARE_TO_QQ_FLAG_QZONE_AUTO_OPEN
        shareParams.putInt(QQShare.SHARE_TO_QQ_EXT_INT,
                QQShare.SHARE_TO_QQ_FLAG_QZONE_AUTO_OPEN);
        doShareToQQ(shareParams);
    }
//qq分享
private void doShareToQQ(final Bundle params) {
    // QQ分享要在主线程做
    ThreadManager.getMainHandler().post(new Runnable() {

        @Override
        public void run() {
            if (null != mTencent) {
                mTencent.shareToQQ(NewBigRecycleActivity.this, params, qqShareListener);
            }
        }
    });
}IUiListener qqShareListener = new IUiListener() {
    @Override
    public void onCancel() {

            Toast.makeText(NewBigRecycleActivity.this, "取消分享", Toast.LENGTH_SHORT).show();

    }
    @Override
    public void onComplete(Object response) {
        // TODO Auto-generated method stub
        Toast.makeText(NewBigRecycleActivity.this, "分享成功", Toast.LENGTH_SHORT).show();

    }
    @Override
    public void onError(UiError e) {
        // TODO Auto-generated method stub
        Log.d("bbbbbb", "onError: 分享错误");
        Toast.makeText(NewBigRecycleActivity.this, "分享错误", Toast.LENGTH_SHORT).show();

    }
};

坑2.QQ分享完后不回调监听的结果....居然还这问题,于是还是百度啦,得知

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //此处必须添加这个回调(解决QQ分享不回调问题)
        Tencent.onActivityResultData(requestCode, resultCode, data, new IUiListener() {
            @Override
            public void onComplete(Object o) {
                Toast.makeText(NewBigRecycleActivity.this, "分享成功", Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onError(UiError uiError) {
                Toast.makeText(NewBigRecycleActivity.this, "分享错误", Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onCancel() {
                Toast.makeText(NewBigRecycleActivity.this, "取消分享", Toast.LENGTH_SHORT).show();

            }
        });
    }

坑3.QQ分享完后总是回调Cancle这个回调方法

初始化Tentcent的Appid是数字,而配置清单中的是tentcent+APPId,如果不一致就会总是回调cancle这个回调

安卓分享之QQ纯大图分享好友和空间_第1张图片

安卓分享之QQ纯大图分享好友和空间_第2张图片

就完了,难怪QQ分享用的人少,坑那么多,文档还停留在17年的,幸好有广大安卓前辈铺路...

我这也是做个做个记录,方便后面的同学少走弯路,一步集成到位...

安排......

你可能感兴趣的:(安卓)