android 测试分享图片+文字到微博
来源:互联网 发布:java中md5加密怎么写 编辑:程序博客网 时间:2024/04/30 15:04
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; }}