Table layout to bitmap
Asked Answered
B

2

0

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 :"));
                    }
Baseball answered 11/3, 2014 at 11:0 Comment(2)
Pls post bitmap conversion code.Futuristic
Did you get any exceptions?Futuristic
D
0

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" />
Dulci answered 11/3, 2014 at 11:45 Comment(3)
Thanks!!it works...One Q though , why is the bitmap image outputs in black?Baseball
@Baseball Means your image in Black screen?Dulci
May be set in xml file or because of Theme. from this answer have you got your output properly?Dulci
F
0

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);
Futuristic answered 11/3, 2014 at 11:36 Comment(0)
D
0

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" />
Dulci answered 11/3, 2014 at 11:45 Comment(3)
Thanks!!it works...One Q though , why is the bitmap image outputs in black?Baseball
@Baseball Means your image in Black screen?Dulci
May be set in xml file or because of Theme. from this answer have you got your output properly?Dulci

© 2022 - 2024 — McMap. All rights reserved.