将您的图像和文本包装到相对布局或约束布局或框架布局中,截取该相对或约束或框架布局的屏幕截图,并分享它,使用This Library 截取您的视图的屏幕截图。
这里有一些代码给你
Bitmap bitmap = ScreenShott.getInstance().takeScreenShotOfJustView(yourview);
File sharedFile = FileUtility.shareImageFile(bitmap);
if (sharedFile != null) {
Uri uri = FileProvider.getUriForFile(context, context.getPackageName().concat(".provider"), sharedFile);
Intent intent = new ShareCompat.IntentBuilder(context)
.setType(context.getContentResolver().getType(uri))
.setStream(uri)
.getIntent();
intent.setAction(Intent.ACTION_SEND);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(intent, "share"));
}
FileUtility shareImageFile 方法
public static File shareImageFile(Bitmap bitmap) {
String rootDirectory = ResourceProvider.get().getContext().getExternalCacheDir() + File.separator + Environment.DIRECTORY_PICTURES + File.separator;
File rootFile = new File(rootDirectory);
if (!rootFile.exists()) {
boolean make = rootFile.mkdirs();
Log.d(TAG, "shareImageFileMakeStatus: " + make);
}
String imagePath = rootDirectory.concat("image_").concat(currentDate()).concat(".png");
try {
FileOutputStream fos = new FileOutputStream(imagePath);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
return new File(imagePath);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
我使用外部 Cashe 目录,因为不需要从用户那里获得此路径的存储权限。
您必须在 res 中添加 xml 文件夹并为文件提供程序创建 .xml 文件的文件提供程序
res/xml/provide_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external_files"
path="." />
</paths>
在您的 manifest.xml 中添加此提供程序
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>