Wrap your image and text into a relative layout or constraint layout or a frame layout, take a screenshot of that relative or constraint or frame layout, and share it, use This Library to take a screen shot of your view.
here is some code for you
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 method
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;
}
}
I use external cashe dir because there is no need to get storage permission from user for this path.
file provider that you must add xml folder in your res and create a .xml file for file provider
res/xml/provide_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external_files"
path="." />
</paths>
add this provider in your 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>