Android 調(diào)用系統(tǒng)分享網(wǎng)絡(luò)圖片到微信

最近項(xiàng)目上有這樣一個(gè)需求,需要分享網(wǎng)絡(luò)圖片到微信,在網(wǎng)上查閱了相關(guān)資料,然后結(jié)合自己的思路,總結(jié)了一下實(shí)現(xiàn)的具體流程:

1、將網(wǎng)絡(luò)圖片下載下來(lái),轉(zhuǎn)換成Bitmap:
  /**
     * @param  path 網(wǎng)絡(luò)圖片地址
     */
    public Bitmap getBitmap(String path) {
        try {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setRequestMethod("GET");
            if (conn.getResponseCode() == 200) {
                InputStream inputStream = conn.getInputStream();
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                return bitmap;
            }
        } catch (IOException e) {

            e.printStackTrace();
        }
        return null;
    }
2、將Bitmap格式的文件保存在本地:
  /**
     * 保存文件
     */
    public void saveFile(Bitmap bm) throws IOException {
        File sd = Environment.getExternalStorageDirectory();
        File dirFile = new File(sd.getPath() + "/Download/imgurl.jpg");
        FileOutputStream fos;
        fos = new FileOutputStream(dirFile);
        if (fos!=null ) {
            bm.compress(Bitmap.CompressFormat.JPEG, 50, fos);
            fos.flush();
            fos.close();
        }
    }
3、調(diào)用系統(tǒng)分享:
 private Uri U;
    private void shareCode() {
        File sd = Environment.getExternalStorageDirectory();
        String path = sd.getPath() + "/Download/imgurl.jpg";
        File file = new File(path);
        if (Build.VERSION.SDK_INT < 24) {
            U = Uri.fromFile(file);
        } else {
            //android 7.0及以上權(quán)限適配
            U= FileProvider.getUriForFile(this,
                    "cn.izis.whteachtest.provider",
                    file);
        }
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_SUBJECT, "分享");
        intent.putExtra(Intent.EXTRA_STREAM, U);
        startActivity(Intent.createChooser(intent, getTitle()));
    }
注意:android 6.0系統(tǒng)以上,需要申請(qǐng)相關(guān)的的權(quán)限,這里就不做具體的說(shuō)明了。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容