android 测试分享图片+文字到微博
2013-11-11 18:54
417 查看
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; } }
相关文章推荐
- 使用Android用自带系统分享功能(填坑,包括微博分享,多图片分享+文字)
- Android组件学习笔记(分享文字以及图片功能)
- Android使用Intent一键分享图片文字到腾讯、新浪、开心、微信等
- Android应用实现微信分享(微信好友,朋友圈),分享链接,图片,文字
- android 系统自带分享文字+图片到微信朋友圈
- 安卓(Android)开发之分享带文字的图片
- Android 调用系统的分享[完美实现同一时候分享图片和文字]
- Android 调用系统的分享[完美实现同时分享图片和文字]
- Android:新浪微博拉起客户端分享——完美实现同时分享图片和文字(Intent.ACTION_SEND)
- Android实战之 上传头像 和仿QQ空间、微博发表文字和图片
- Android APP 分享图片文字到微信刚开始正常,后面就不弹出分享框了
- Android APP 分享图片文字到微信刚開始正常,后面就不弹出分享框了
- Android 如何将图片和文字同时分享到微信
- Android Intent分享文件|分享图片|分享文字|功能的实现
- Android 如何将图片和文字同时分享到微信
- android分享图片文字
- android 使用Intent.ACTION_SEND分享图片和文字内容(新浪微博,短信等)
- android camer 图片回显界面照片分享到微博、人人、彩信、蓝牙的功能实现
- iOS 微博 sdk 分享文字图片
- Android系统自带分享功能的实现(可同时分享文字和图片)