android分享图片到微信,无需申请APP_ID

 

你只需要一个工具类,就可以马上实现分享,快捷简单高效,省去了很多集成的麻烦!

当然,如果你想集成腾讯的SDK,也可以看我写的集成文章;

android 微信分享集成大坑总结

 

工具类:

实现功能:

本地路径:将图片发送到朋友

本地路径:将图片发送到朋友圈

将bitmap发送到朋友

将bitmap发送到朋友圈

public class ShareTools {

    private static final String[] PERMISSION_EXTERNAL_STORAGE = new String[]{
            Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
    private static final int REQUEST_EXTERNAL_STORAGE = 100;

    //本地路径:将图片发送到朋友
    public static void shareToWeChatFriend(Activity activity, String filePath) {

        if (isWeixinAvilible(activity)){

            //authority 需要替换,还需要有SD卡权限
            Uri uriForFile = FileProvider.getUriForFile(activity, "com.ksk.android.fileProvider", new File(filePath));
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.putExtra(Intent.EXTRA_STREAM, uriForFile);
            intent.putExtra("android.support.v4.app.EXTRA_CALLING_PACKAGE", activity.getPackageName());
            intent.putExtra("android.support.v4.app.EXTRA_CALLING_ACTIVITY", activity.getComponentName());
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
            intent.setType(activity.getContentResolver().getType(uriForFile));
            intent.setClassName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            boolean hasPermission = true;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                hasPermission = activity.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
            }
            if (!hasPermission) {
                Toast.makeText(activity,"请允许权限,否则无法分享!",Toast.LENGTH_SHORT).show();
                verifyStoragePermissions(activity);
            } else {
                activity.startActivity(intent);
            }
        }else {
            //没有安装微信
            Toast.makeText(activity,"请先安装微信",Toast.LENGTH_SHORT).show();
        }


    }

    //本地路径:将图片发送到朋友圈
    public static void shareToWeChatTimeLine(Activity activity, String filePath) {

        if (isWeixinAvilible(activity)){
            //authority 需要替換,还需要有SD卡权限
            Uri uriForFile = FileProvider.getUriForFile(activity, "com.ksk.android.fileProvider", new File(filePath));
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.putExtra("android.support.v4.app.EXTRA_CALLING_PACKAGE", activity.getPackageName());
            intent.putExtra("android.support.v4.app.EXTRA_CALLING_ACTIVITY", activity.getComponentName());
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
            intent.putExtra(Intent.EXTRA_STREAM, uriForFile);
            intent.setType(activity.getContentResolver().getType(uriForFile));
            intent.setClassName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            boolean hasPermission = true;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                hasPermission = activity.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
            }
            if (!hasPermission) {
                Toast.makeText(activity,"请允许权限,否则无法分享!",Toast.LENGTH_SHORT).show();
                verifyStoragePermissions(activity);
            } else {
                activity.startActivity(intent);
            }
        }else {
            //没有安装微信
            Toast.makeText(activity,"请先安装微信",Toast.LENGTH_SHORT).show();
        }

    }

    public static void shareBmpToWeChatFriend(Activity context,Bitmap bmp){
        String uri = saveImageToGallery(bmp);
        shareToWeChatFriend(context,uri);

    }

    public static void shareBmpToWeChatTimeLine(Activity context,Bitmap bmp){
        String uri = saveImageToGallery(bmp);
        shareToWeChatTimeLine(context,uri);

    }

    public static String saveImageToGallery(Bitmap bmp) {
        String root = Environment.getExternalStorageDirectory().getAbsolutePath();
        String dirName = "sky";
        File appDir = new File(root, dirName);
        if(!appDir.exists()) {
            appDir.mkdirs();
        }

        long timeStamp = System.currentTimeMillis();
        String fileName = timeStamp + ".jpg";
        File file = new File(appDir, fileName);
        FileOutputStream fos = null;

        try {
            fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            //发送广播刷新
            //context.sendBroadcast(new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE", Uri.fromFile(new File(file.getPath()))));
            byte var10 = 2;
            return file.getPath();
        } catch (FileNotFoundException var22) {
            var22.printStackTrace();
        } catch (IOException var23) {
            var23.printStackTrace();
        } finally {
            try {
                if(fos != null) {
                    fos.close();
                }
            } catch (IOException var21) {
                var21.printStackTrace();
            }

        }

        return null;
    }

    //是否安装微信
    private static boolean isWeixinAvilible(Context context) {
        final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
        List pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mm")) {
                    return true;
                }
            }
        }

        return false;
    }

    //动态获取权限
    private static boolean verifyStoragePermissions(Activity activity) {
        int permissionWrite = ActivityCompat.checkSelfPermission(activity,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (permissionWrite != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(activity, PERMISSION_EXTERNAL_STORAGE,
                    REQUEST_EXTERNAL_STORAGE);
            return false;
        } else {
            return true;
        }
    }

}

不要忘记了,清单文件添加权限:

res目录添加xml文件夹,并在xml文件夹中添加 file_paths.xml 文件,内容为:



    
    

清单文件继续添加:


            
        

完成!

 

你可能感兴趣的:(android日常开发)