Android 无需申请key直接调用微信/QQ/微博分享

无需申请key直接调用微信/QQ/微博分享
支持单图,多图,文字,单图+文字,多图+文字
具体还需要看情况,自己试一下

package com.yaphetzhao.library_shareanywhere;

import android.Manifest;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.view.View;

import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

import java.util.ArrayList;
import java.util.Collections;

/**
 * Created by yaphet
 * on 2018/3/21.
 */

public class ShareAnyWhereUtil {

    /**
     * bitmapToUri
     *
     * @param context
     * @param bitmap
     * @return
     */
    public static Uri bitmapToUri(Activity context, Bitmap bitmap) {
        if (!checkPermission(context)) {
            return null;
        }
        return Uri.parse(MediaStore.Images.Media.insertImage(context.getContentResolver(), bitmap, null, null));
    }

    /**
     * drawableToUri
     *
     * @param context
     * @param drawable
     * @return
     */
    public static Uri drawableToUri(Activity context, Drawable drawable) {
        if (!checkPermission(context)) {
            return null;
        }
        return Uri.parse(MediaStore.Images.Media.insertImage(context.getContentResolver(), ((BitmapDrawable) drawable).getBitmap(), null, null));
    }

    /**
     * viewToUri
     *
     * @param context
     * @param view
     * @return
     */
    public static Uri viewToUri(Activity context, View view) {
        if (!checkPermission(context)) {
            return null;
        }
        view.buildDrawingCache();
        return Uri.parse(MediaStore.Images.Media.insertImage(context.getContentResolver(), view.getDrawingCache(), null, null));
    }

    /**
     * createUriList
     *
     * @param uris
     * @return
     */
    public static ArrayList createUriList(Uri... uris) {
        ArrayList result = new ArrayList<>();
        Collections.addAll(result, uris);
        return result;
    }

    /**
     * 分享到Weichat
     *
     * @param context
     * @param uris
     * @param desc
     */
    public static void shareWeichat(Activity context, ArrayList uris, String desc) {
        if (!checkPermission(context)) {
            return;
        }
        Intent intent = new Intent();
        ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
        intent.setComponent(comp);
        intent.setAction(Intent.ACTION_SEND_MULTIPLE);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Intent.ACTION_SEND_MULTIPLE);
        intent.setType("image/*");
        intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
        intent.putExtra("Kdescription", desc);
        context.startActivity(intent);
    }

    /**
     * 分享到新浪微博
     *
     * @param context
     * @param uris
     * @param desc
     */
    public static void shareWeibo(Activity context, ArrayList uris, String desc) {
        if (!checkPermission(context)) {
            return;
        }
        Intent intent = new Intent();
        intent.setPackage("com.sina.weibo");
        intent.setAction(Intent.ACTION_SEND_MULTIPLE);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Intent.ACTION_SEND_MULTIPLE);
        intent.setType("image/*");
        intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
        intent.putExtra("Kdescription", desc);
        context.startActivity(intent);
    }

    /**
     * 分享到QQ
     *
     * @param context
     * @param uris
     * @param desc
     */
    public static void shareQQ(Activity context, ArrayList uris, String desc) {
        if (!checkPermission(context)) {
            return;
        }
        Intent intent = new Intent();
        ComponentName comp = new ComponentName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");
        intent.setComponent(comp);
        intent.setAction(Intent.ACTION_SEND_MULTIPLE);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Intent.ACTION_SEND_MULTIPLE);
        intent.setType("image/*");
        intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
        intent.putExtra("Kdescription", desc);
        context.startActivity(intent);
    }

    /**
     * 权限检查
     *
     * @param context
     * @return
     */
    private static boolean checkPermission(Activity context) {
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
                ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                String[] mPermissionList = new String[]{
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE};
                ActivityCompat.requestPermissions(context, mPermissionList, 1);
                return false;
            }
        }
        return true;
    }

}

你可能感兴趣的:(Android 无需申请key直接调用微信/QQ/微博分享)