1. 程式人生 > >Android 系統分享圖片,文字

Android 系統分享圖片,文字

             使用系統自帶分享功能,分享圖片文字

1、分享文字

//分享文字
public void shareText() {
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_TEXT, "This is my Share text.");
    shareIntent.setType("text/plain");

    //設定分享列表的標題,並且每次都顯示分享列表
    startActivity(Intent.createChooser(shareIntent, "分享到"));
}

2、分享網路圖片

//分享單張圖片
public void shareSingleImage() {
    //由檔案得到uri
    Uri imageUri = Uri.parse(MediaStore.Images.Media.insertImage(getActivity().getContentResolver(), bitmap, null, null));
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
    shareIntent.setType("image/*");
    startActivity(Intent.createChooser(shareIntent, "分享到"));
}

---------------------      url 轉 bitmap        ------------------------

/**
     * 非同步執行緒下載圖片
     */
    Bitmap bitmap;

    /**
     * 獲取網路圖片
     *
     * @param imageurl 圖片網路地址
     * @return Bitmap 返回點陣圖
     */
    public Bitmap GetImageInputStream(String imageurl) {
        URL url;
        HttpURLConnection connection = null;
        Bitmap bitmap = null;
        try {
            url = new URL(imageurl);
            connection = (HttpURLConnection) url.openConnection();
            connection.setConnectTimeout(6000); //超時設定
            connection.setDoInput(true);
            connection.setUseCaches(false); //設定不使用快取
            InputStream inputStream = connection.getInputStream();
            bitmap = BitmapFactory.decodeStream(inputStream);
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    class Task extends AsyncTask<String, Integer, Void> {

        protected Void doInBackground(String... params) {
            bitmap = GetImageInputStream((String) params[0]);
            return null;
        }

        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            Message message = new Message();
            message.what = 0x123;
        }

    }