首页 > 代码库 > 本地分享总结
本地分享总结
- 文章调起来分享:自己程序调起来分享列表
- Intent email = new Intent(android.content.Intent.ACTION_SEND);
- email.setType("text/plain");
- // 设置邮件默认地址
- // email.putExtra(android.content.Intent.EXTRA_EMAIL, "1");
- // 设置邮件默认标题
- email.putExtra(android.content.Intent.EXTRA_SUBJECT,
- "我是邮件的标题");
- // 设置要默认发送的内容
- email.putExtra(android.content.Intent.EXTRA_TEXT,
- "我是分享的内容");
- // 调用系统的邮件系统
- activity.startActivity(Intent.createChooser(email, "分享方式"));
调起来分享处理:接收图库中设置分享
<activity
android:name="com.huika.huixin.control.hxcircle.activity.PublishDynamicActivity"
android:icon="@drawable/ic_launcher"
android:label="@string/share_to_sns"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />(这里只是配置的图片分享,也可以配置文本分享)
</intent-filter>
</activity> /**
* @description:从图库中设置分享;
* @author samy
* @date 2014年9月5日 上午9:40:42
*/
private void initActivitystate(Intent intent) {
if (Intent.ACTION_SEND.equals(intent.getAction())) {
Bundle extras = intent.getExtras();
final String mimeType = intent.getType();
if (extras.containsKey(Intent.EXTRA_STREAM)) {
Uri uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);
if (uri != null && !TextUtils.isEmpty(uri.toString()) && mimeType.startsWith("image/")) {
String path = ImageTools.uriConvert2path(this, uri);
if (!TextUtils.isEmpty(path)) {
Bimp.clearBimp();
File uploadFile = ImageTools.saveImgForUpload(path);
Bimp.drr.add(uploadFile.getAbsolutePath());
}
}
else {
Toast.makeText(this, "分享失败", Toast.LENGTH_SHORT).show();
}
}
}
} public static String uriConvert2path(Context c, Uri uri) {
String imagePath = null;
String uriStr = uri.toString();
if (uriStr.startsWith("file://")) {
imagePath = uriStr.replaceFirst("file://", "");
}
else {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = c.getContentResolver().query(uri, proj, null, null, null);
if (cursor != null) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
imagePath = cursor.getString(column_index);
cursor.close();
}
}
return imagePath;
} /**
* 保存拍照后的图片,用于上传
*/
public static File saveImgForUpload(String tempFilePath) {
BitmapFactory.Options opts = new BitmapFactory.Options();// 获取缩略图显示到屏幕上
opts.inJustDecodeBounds = true;
BitmapFactory.decodeFile(tempFilePath, opts);
int srcSize = opts.outHeight * opts.outWidth;
if (srcSize > UPLOAD_IMG_SIZE) {// 超过最大值
opts.inSampleSize = computeSampleSize(opts, -1, UPLOAD_IMG_SIZE);
}
else {
opts.inSampleSize = 1;
}
opts.inJustDecodeBounds = false;
// 拿到之前旋转的角度
int degree = readPictureDegree(tempFilePath);
if (opts.inSampleSize == 1 && degree == 0) {// 既没有旋转也没有超过大小,直接上传原图
return new File(tempFilePath);
}
// 旋转图片 动作
Matrix matrix = new Matrix();
matrix.postRotate(degree);
Bitmap bitmap = null;
Bitmap resizedBitmap = null;
File picFile = null;
FileOutputStream fos = null;
try {
bitmap = BitmapFactory.decodeFile(tempFilePath, opts);
// 创建新的图片
resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
try {
picFile = initTempFile();
fos = new FileOutputStream(picFile);
if (opts.inSampleSize > 1 && opts.inSampleSize <= 4) {// 测试结果
int rate = (int) (100 * (1 - (srcSize - UPLOAD_IMG_SIZE) * 0.2 / UPLOAD_IMG_SIZE));
rate = Math.max(rate, 50);
rate = Math.min(rate, 100);
resizedBitmap.compress(Bitmap.CompressFormat.JPEG, rate, fos);
}
else {
int divide = opts.inSampleSize * UPLOAD_IMG_SIZE;
int rate = (int) (100 * (1 - (srcSize - divide) * 0.015 / divide));
rate = Math.max(rate, 50);
rate = Math.min(rate, 100);
resizedBitmap.compress(Bitmap.CompressFormat.JPEG, rate, fos);
}
fos.flush();
}
catch (Exception e) {
e.printStackTrace();
}
}
finally {
if (bitmap != null)
bitmap.recycle();
if (resizedBitmap != null)
resizedBitmap.recycle();
if (fos != null)
try {
fos.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
return picFile;
}
来自为知笔记(Wiz)
本地分享总结
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。