表布局位图 [英] Table layout to bitmap

查看:138
本文介绍了表布局位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个形式 TableLayout 其中的形式是从我的数据库填补,我想通过email.But送表输出我有问题转换tablelayout以位图。这里是我工作的最后一个活动。

 公共类SendEmail延伸活动{
    按钮buttonSend;
    TableLayout tableMessage;
    意图emailFinal;
     公共无效的onCreate(捆绑savedInstanceState){
            super.onCreate(savedInstanceState);            的setContentView(R.layout.sendemail);             订购日期字符串= java.text.DateFormat.getDateTimeInstance()格式(Calendar.getInstance()的getTime()。)。             字符串email = pdatabase.getPEmail();
             emailFinal =新意图(Intent.ACTION_SEND);
             emailFinal.putExtra(Intent.EXTRA_EMAIL,新的String [] {电子邮件});

buttonSend =(按钮)findViewById(R.id.sendEmail);

tableMessage =(TableLayout)findViewById(R.id.tableLayout1);

  buttonSend.setOnClickListener(新OnClickListener(){                    @覆盖
                    公共无效的onClick(视图v){
                    位图B = Bitmap.createBitmap(tableMessage.getWidth(),
                            tableMessage.getHeight(),
                               Bitmap.Config.ARGB_8888);
            帆布C =新的Canvas(B);
            tableMessage.draw(C);
            BitmapDrawable D =新BitmapDrawable(getResources(),B)
                        emailFinal.putExtra(Intent.EXTRA_TEXT,B);
                        emailFinal.setType(信息/ RFC822);
                        startActivity(Intent.createChooser(emailFinal,选择电子邮件客户端));
                    }


解决方案

您可以简单的使用这一点。

 公共无效sendMyData(视图v){ 位图CS = NULL;
 tableMessage.setDrawingCacheEnabled(真);
 tableMessage.buildDrawingCache(真);
 CS = Bitmap.createBitmap(tableMessage.getDrawingCache());
 帆布帆布=新的Canvas(CS);
 tableMessage.draw(画布);
 canvas.save();
 tableMessage.setDrawingCacheEnabled(假);
 字符串路径= Images.Media.insertImage(getContentResolver(),CS,
                MyTableOutput,NULL);
 URI URI = Uri.parse(路径);
 意图sharingIntent =新意图(Intent.ACTION_SEND);
 sharingIntent.setType(图像/ PNG);
 sharingIntent.putExtra(Intent.EXTRA_STREAM,URI);
 startActivity(Intent.createChooser(sharingIntent,
                    共享图像使用));}

现在在XML文件中SE这为您的按钮。

 安卓的onClick =sendMyData

添加权限清单文件也。

 <使用许可权的android:NAME =android.permission.READ_EXTERNAL_STORAG​​E/>
 <使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAG​​E/>

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 :"));
                    }

解决方案

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" />

这篇关于表布局位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆