问题
I have a form in a TableLayout
where the form is filled from my database, I want to send the table output via email.But I have problem with converting the tablelayout to Bitmap. Here is the final activity I am working on
public class SendEmail extends Activity {
Button buttonSend;
TableLayout tableMessage;
Intent emailFinal;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sendemail);
String Orderdate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
String email = pdatabase.getPEmail();
emailFinal = new Intent(Intent.ACTION_SEND);
emailFinal.putExtra(Intent.EXTRA_EMAIL, new String[]{ email});
buttonSend =(Button) findViewById(R.id.sendEmail);
tableMessage = (TableLayout) findViewById(R.id.tableLayout1);
buttonSend.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Bitmap b = Bitmap.createBitmap( tableMessage.getWidth(),
tableMessage.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
tableMessage.draw(c);
BitmapDrawable d = new BitmapDrawable(getResources(), b)
emailFinal.putExtra(Intent.EXTRA_TEXT, b);
emailFinal.setType("message/rfc822");
startActivity(Intent.createChooser(emailFinal, "Choose an Email client :"));
}
回答1:
you can simple use this.
public void sendMyData(View v){
Bitmap cs = null;
tableMessage.setDrawingCacheEnabled(true);
tableMessage.buildDrawingCache(true);
cs = Bitmap.createBitmap(tableMessage.getDrawingCache());
Canvas canvas = new Canvas(cs);
tableMessage.draw(canvas);
canvas.save();
tableMessage.setDrawingCacheEnabled(false);
String path = Images.Media.insertImage(getContentResolver(), cs,
"MyTableOutput", null);
Uri uri = Uri.parse(path);
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("image/png");
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(sharingIntent,
"Share image using"));
}
Now in your xml file se this for your Button.
android:onClick = "sendMyData"
Add permission to manifest file also.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
回答2:
Try this one.
tableMessage.setDrawingCacheEnabled(true);
tableMessage.layout(0, 0, tableMessage.getWidth(), tableMessage.getHeight());
tableMessage.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(tableMessage.getDrawingCache());
tableMessage.setDrawingCacheEnabled(false);
来源:https://stackoverflow.com/questions/22323720/table-layout-to-bitmap