Android-分享

现在基本上都是android7.0以上了吧
在 app - AndroidManifest - application中添加

<provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

file_paths.xml 放在/res/xml/

我这边是这样的

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <root-path
        name="root"
        path=""/>
    <files-path
        name="files"
        path=""/>

    <cache-path
        name="cache"
        path=""/>
    <external-files-path
        name="external_file_path"
        path=""/>
    <external-cache-path
        name="external_cache_path"
        path=""/>
    <external-path
        name="external"
        path="phonelive"/>
</paths>

  <!--external-path 标签用来指定Uri共享的,name 属性的值可以自定义,
    path属性的值表示共享的具体位置,设置为空,就表示共享整个SD卡-->

搞几个例子吧

调用系统分享

/**
     * 分享文本
     *
     * @param context
     * @param text
     * @param title
     */
    public static void sendText(Context context, String text, String title) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT, text);
        context.startActivity(Intent.createChooser(intent, title));
    }

    /**
     * 分享图片
     *
     * @param context
     * @param uri
     * @param title
     */
    public static void sendImage(Context context, Uri uri, String title) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("image/png");
        intent.putExtra(Intent.EXTRA_STREAM, uri);
        context.startActivity(Intent.createChooser(intent, title));
    }

    /**
     * 分享多张图片
     *
     * @param context
     * @param imageUris
     * @param title
     */
    public static void sendMoreImage(Context context, ArrayList<Uri> imageUris, String title) {
        Intent mulIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
        mulIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
        mulIntent.setType("image/jpeg");
        context.startActivity(Intent.createChooser(mulIntent, "多图文件分享"));
    }

    /**
     * 分享视频
     *
     * @param context
     * @param uri
     */
    public static void sendVideo(Context context, Uri uri) {
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
        shareIntent.setType("video/*");//选择视频
        context.startActivity(Intent.createChooser(shareIntent, "分享到"));
    }

Uri定义:

Uri photoUri = FileProvider.getUriForFile(
        mContext,
        getPackageName() + ".fileprovider",
        mShareImageFile);

分享指定APP(先判断这个APP是否有安装)

ins

if (!AppInstalledUtils.checkAppInstalled(mContext, "com.instagram.android")) {
ToastUtil.show(getString(com.vemo.video.R.string.not_install) + "INSTATRAM");
return;}

intent.setPackage("com.instagram.android");

facebook

if (!AppInstalledUtils.checkAppInstalled(mContext, "com.facebook.katana")) {
ToastUtil.show(getString(com.vemo.video.R.string.not_install) + "Facebook");
return;}

intent.setPackage("com.facebook.katana");

line

if (!AppInstalledUtils.checkAppInstalled(mContext, "jp.naver.line.android")) {
ToastUtil.show(getString(com.vemo.video.R.string.not_install) + "LINE");
return;}

String scheme = "line://msg/image" + mShareImageFile.getPath();
                Uri uri1 = Uri.parse(scheme);
                startActivity(new Intent(Intent.ACTION_VIEW, uri1));

QQ、微信,同样的,指定包名就可以。建议调用接入SDK。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值