android 测试分享图片+文字到微博

该博客展示了如何在Android应用中实现分享图片和文字到微博的功能。通过创建Bitmap对象,将文字添加到图片上,然后使用Intent ACTION_SEND进行分享。代码中包括了文件操作、Bitmap处理以及Intent设置。
package com.example;

import android.app.Activity;
import android.content.Intent;
import android.graphics.*;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import df.util.Util;
import df.util.android.LogUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class MyActivity extends Activity implements View.OnClickListener {

    private static final String TAG = Util.toTAG(MyActivity.class);

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        findViewById(R.id.share_btn).setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        //To change body of implemented methods use File | Settings | File Templates.

        switch (view.getId()) {
            case R.id.share_btn: {
                share();
                break;
            }
            default: {
                break;
            }
        }
    }

    void share() {

        File file = new File("/sdcard/fk.jpg");

        try {
            Bitmap bmp = getBitmap("/sdcard/fk.jpg", "womencishfiewhfuewjfkwej");
            OutputStream stream = null;
            LogUtil.v(TAG,"haitag ",bmp.getWidth(),", ",bmp.getHeight());
            stream = new FileOutputStream(new File("/sdcard/tmp.jpg"));
            bmp.compress(Bitmap.CompressFormat.JPEG, 40, stream);
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        Uri uri = Uri.fromFile(new File("/sdcard/fk.jpg"));
        if (uri != null) {
            shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
            shareIntent.setType("image/*");
            //当用户选择短信时使用sms_body取得文字
            shareIntent.putExtra("sms_body", "hehehhehehe ");
        } else {
            shareIntent.setType("text/plain");
        }

        shareIntent.putExtra(Intent.EXTRA_TEXT, "hahahah");
        //自定义选择框的标题
        //startActivity(Intent.createChooser(shareIntent, "邀请好友"));
        //系统默认标题
        startActivity(shareIntent);
    }


    public Bitmap getBitmap(final String picPath, final String msg) {
//返回具有指定宽度和高度可变的位图,它的初始密度可以调用getDensity()

        final int TXT_SIZE = 24;
        final Activity act = this;
        Bitmap bmp = BitmapFactory.decodeFile(picPath);
        int width = bmp.getWidth();
        int heigth = bmp.getHeight();
        List<String> buf = new ArrayList<String>();
        String lineStr = "";
        int lineCharCount = 0;

        for (int i = 0; i < msg.length(); ) {

            if (Character.getType(msg.charAt(i)) == Character.OTHER_LETTER) {
                // 如果这个字符是一个汉字
                if ((i + 1) < msg.length()) {
                    lineStr += msg.substring(i, i + 2);
                    lineCharCount++;
                }

                heigth += TXT_SIZE;
                i = i + 2;
            } else {
                heigth += TXT_SIZE;
                lineStr += msg.substring(i, i + 1);
                lineCharCount++;
                i++;
            }

            if ((lineCharCount + 1) * TXT_SIZE > width) {
                buf.add(lineStr);
                lineStr = "";
                lineCharCount = 0;
            }

            if (i >= msg.length()) {
                break;
            }
        }


        Bitmap canvasBmp = Bitmap.createBitmap(width + 20, heigth + 20, Bitmap.Config.ARGB_8888);
//把一个具有指定的位图绘制到画布上。位图必须是可变的。
//在画布最初的目标密度是与给定的位图的密度相同,返回一个具有指定位图的画布
        Canvas canvas = new Canvas(canvasBmp);
//设置画布的颜色
        canvas.drawColor(Color.WHITE);
        Paint p = new Paint();
        Typeface font = Typeface.create("宋体", Typeface.BOLD);
        p.setColor(Color.RED);
        p.setTypeface(font);
        p.setTextSize(TXT_SIZE);

        float y = 0;
        for (String str : buf) {
            canvas.drawText(str, 0, y, p);
            y += TXT_SIZE;
        }

        canvas.drawBitmap(bmp, 0, y, p);
        return bmp;
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值