安卓微信QQ原生分享(不用第三方)>2018.4.10

說(shuō)明

其實(shí)網(wǎng)上已經(jīng)有非常多不用三方的demo了,不過(guò)都沒(méi)有考慮權(quán)限的問(wèn)題,權(quán)限問(wèn)題可參考Android 6.0 動(dòng)態(tài)權(quán)限申請(qǐng),安卓7.0文件存儲(chǔ)權(quán)限變更,我就自己寫(xiě)一個(gè)demo給大家參考,可能會(huì)有點(diǎn)亂,本人小白輕噴

一共三步

1.配置安卓7.0權(quán)限

首先,在AndoridManifest文件下的application下配置

<application>
...
  <provider
              android:name="android.support.v4.content.FileProvider"
              android:authorities="com.xxx.fileprovider"
              android:exported="false"
              android:grantUriPermissions="true">
              <meta-data
                 android:name="android.support.FILE_PROVIDER_PATHS"
                  android:resource="@xml/file_paths" />
          </provider>
...
</application>

android:authorities一般是包名+fileprovider,然后在res下創(chuàng)建xml文件,添加filepaths.xml文件


filepaths文件
<path>
    <external-path
        name="getExternalStorageDirectory_path"
        path="./storage/emulated/0/" />
    <root-path
        name="root_path"
        path="." />
</path>

這里我只配置了倆個(gè),第一個(gè)表示在App內(nèi)置存儲(chǔ)區(qū)域下的緩存文件,它和 getExternalFilesDir()返回的路徑一樣,第二個(gè)為根路徑,更多的配置請(qǐng)點(diǎn)擊文章開(kāi)頭的超鏈接。

另外還配置權(quán)限
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
還得申請(qǐng)權(quán)限(安卓6.0動(dòng)態(tài)權(quán)限和安卓7.0訪問(wèn)文件權(quán)限),真麻煩
private String[] permission = {Manifest.permission.READ_EXTERNAL_STORAGE};
    //檢查版本,大于23就申請(qǐng)權(quán)限
    private void checkPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // 檢查該權(quán)限是否已經(jīng)獲取
            int i = ContextCompat.checkSelfPermission(this, permission[0]);
            // 權(quán)限是否已經(jīng) 授權(quán) GRANTED---授權(quán)  DINIED---拒絕
            if (i != PackageManager.PERMISSION_GRANTED) {
                //無(wú)權(quán)限,準(zhǔn)備申請(qǐng)權(quán)限
                ActivityCompat.requestPermissions(this, permission, 321);
             }
    }

    //判斷是否大于安卓7.0
    private Uri isSeven(File file, Intent intent) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            //大于7.0,應(yīng)使用該路徑
            uri = FileProvider.getUriForFile(ShareDemo.this, "com.xxx.fileprovider", file);
            // 給目標(biāo)應(yīng)用一個(gè)臨時(shí)授權(quán)
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } else {
            //小于7.0
            uri = Uri.fromFile(file);
        }
        return uri;
    }

2.準(zhǔn)備圖片

終于可以開(kāi)始分享了,你可以用各種圖片,我這里就拿安卓的icon了

private File file,imgFile,dir;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.share_layout);
        ImageView iv = (ImageView) findViewById(R.id.iv);
        //資源文件轉(zhuǎn)換為bitmap
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        //創(chuàng)建文件,指定文件夾名為"iv"
        dir = new File(Environment.getExternalStorageDirectory(), "iv");
        if (!dir.exists()) {
            dir.mkdir();
        }
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //分享至微信朋友
//                share(imgFile);
                //分享至微信朋友圈
                shareToCommunity(imgFile);
                //分享至QQ好友
//                shareToQQFriend();
            }
        });
        //拼接路徑并將圖片命名為"System.currentTimeMillis().jpg"
        file = new File(dir, System.currentTimeMillis() + ".jpg");
        //把bitmap轉(zhuǎn)file
        imgFile = bitmapToFile(bmp, file);
    }


    //把Bitmap轉(zhuǎn)換成File
    public File bitmapToFile(Bitmap bitmap, File file) {
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

3.終于到分享了

    //分享圖片 到微信朋友
    private void share(File file) {
        Intent intent = new Intent();

        //判斷是否大于安卓7.0
        Uri uri = isSeven(file, intent);

        ComponentName componentName = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
        intent.setComponent(componentName);
        intent.setAction(Intent.ACTION_SEND);
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_STREAM, uri);

        startActivity(intent);
    }

    //分享圖片 至朋友圈
    private void shareToCommunity(File file) {
        Intent intent = new Intent();

        //判斷是否大于安卓7.0
        Uri uri = isSeven(file, intent);

        ComponentName componentName = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
        intent.setComponent(componentName);
        intent.setAction(Intent.ACTION_SEND);
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_STREAM, uri);

        startActivity(intent);
    }

    //分享文本 到QQ好友(微信,朋友圈同理,這里分享文本不涉及訪問(wèn)文件就不用判斷安卓是否大于7.0了)
    private void shareToQQFriend() {
        Intent intent = new Intent();
        ComponentName componentName = new ComponentName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");
        intent.setComponent(componentName);
        intent.setAction(Intent.ACTION_SEND);
        intent.setType("text/*");
        intent.putExtra(Intent.EXTRA_TEXT, "這是分享內(nèi)容");
        startActivity(intent);
    }

這里最好判斷一下用戶是否安裝了微信,QQ

    //判斷微信是否安裝(判斷QQ改包名就行啦"com.tencent.mobileqq")
    public static boolean isWeixinAvilible(Context context) {
        // 獲取packagemanager
        final PackageManager packageManager = context.getPackageManager();
        // 獲取所有已安裝程序的包信息
        List<PackageInfo> 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;
    }

大功告成!看看效果圖↓

源碼在這↓

源碼

參考文章:
Android 6.0 動(dòng)態(tài)權(quán)限申請(qǐng)
安卓7.0文件存儲(chǔ)權(quán)限變更

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。