I have my layout with information about a transaction this info I want to share with the design that my view has, I want to do it converting the layout to image and share it via wsap, mail or according to the app that is chosen.
I was able to find that to convert a layout to an image one will first have to convert to bitmap and then the bitmap convert to an image in order to share it. I could find a code that did this but what it does is take the image of the root of your app and share it.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_compartir);
botonCompartir = (Button) findViewById(R.id.buttonShare);
String ic_android_green = createImageOnSDCard(R.drawable.prueba);//imagen
botonCompartir.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri path = FileProvider.getUriForFile(compartirActivity.this, "jedu.domain.prototipo02", new File(ic_android_green));//jedu.domain.prototipo02 id del app
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "This is one image I'm sharing.");
shareIntent.putExtra(Intent.EXTRA_STREAM, path);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share..."));
}
});
}
private String createImageOnSDCard(int resID) {//
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resID);
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/" + resID + ".png";
File file = new File(path);
try {
OutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
Log.e("APPFIREBASE 0: ", "file.getPath() " + file.getPath());
return file.getPath();
}
Clicking shares the image that is stored in the root.