android分享文字加图片,android简单分享----文字加图片

这篇博客介绍了如何在Android应用中实现从当前屏幕截图并分享到微博等社交平台的功能。通过创建Bitmap对象,捕获屏幕内容,保存为图片文件,然后创建Intent分享到其他应用。分享过程依赖于目标社交应用是否已安装在手机上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这是android的简单分享,如果想分享到微博这些的话,需要你的手机里安装相关的app,下面的这些代码是整理网上各种网友的代码而得来的,感谢分享!

使用这个类的方法:

1、肯定是导入下面这个类啦

2、在你的Activity加入下面几行代码

WindowManager windowManager = getWindowManager();

View decorview = YourActivity.this .getWindow().getDecorView();

ShareToSNS my_share = new ShareToSNS();

startActivity(Intent. createChooser( my_share.getIntentSharePhotoAndText( my_share.GetandSaveCurrentImage(windowManager,decorview),"发送到编辑框的文本内容"), "分享" ));

类的代码

/**

*这是分享文字加图片的类

*/

import java.io.File;

import java.io.FileOutputStream;

import android.app.Activity;

import android.content.Intent;

import android.graphics.Bitmap;

import android.graphics.Bitmap.Config;

import android.net.Uri;

import android.os.Environment;

import android.view.Display;

import android.view.View;

import android.view.WindowManager;

import android.widget.Toast;

public class ShareToSNS {

private String SavePath;

private String image_name = "/Scinan_Screen.png";

public String GetandSaveCurrentImage(WindowManager windowManager,View decorview)

{

//1.构建Bitmap

Display display = windowManager.getDefaultDisplay();

int w = display.getWidth();

int h = display.getHeight();

Bitmap Bmp = Bitmap.createBitmap( w, h, Config.ARGB_8888 );

//2.获取屏幕

decorview.setDrawingCacheEnabled(true);

Bmp = decorview.getDrawingCache();

SavePath = getSDCardPath()+"/AndyDemo/ScreenImage";

//3.保存Bitmap

try {

File path = new File(SavePath);

//文件

String filepath = SavePath + image_name;

File file = new File(filepath);

if(!path.exists()){

path.mkdirs();

}

if (!file.exists()) {

file.createNewFile();

}

FileOutputStream fos = null;

fos = new FileOutputStream(file);

if (null != fos) {

Bmp.compress(Bitmap.CompressFormat.PNG, 90, fos);

fos.flush();

fos.close();

}

} catch (Exception e) {

}

return SavePath;

}

private String getSDCardPath(){

File sdcardDir = null;

//判断SDCard是否存在

boolean sdcardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

if(sdcardExist){

sdcardDir = Environment.getExternalStorageDirectory();

}

return sdcardDir.toString();

}

// 分享照片

public Intent getIntentSharePhotoAndText(String photoUri,String share_text) {

Intent shareIntent = new Intent(Intent.ACTION_SEND);

File file = new File(photoUri + image_name);

shareIntent.putExtra(Intent.EXTRA_TEXT, share_text);

shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));

shareIntent.setType("image/png");

return shareIntent;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值