我获取数据并希望转换为PDF格式,应附在电子邮件中

时间:2015-02-10 06:34:48

标签: android

为此,我获取了存储在sqlite中的活动中的所有数据。现在所有数据都应转换为pdf格式,并自动附加到电子邮件中。当我按分享按钮。如果有的话,请给我你宝贵的建议,链接或完整的源代码。

public class ListofShopping extends Activity {


    TextView cat, prd, iName, iBrand, iAge, iColor, iQnty, iCst, iDsc;
    Button discard, share;
    String s1, s2, s3, s4, s5, s6, s7, s8, s9, a1, a2;
    SQLiteDatabase db;
    Cursor c;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.listofshop);


        cat     = (TextView) findViewById(R.id.eCat);
        prd     = (TextView) findViewById(R.id.ePrdct);
        iName   = (TextView) findViewById(R.id.eItem);
        iBrand  = (TextView) findViewById(R.id.eBrand);
        iAge    = (TextView) findViewById(R.id.eAge);
        iColor  = (TextView) findViewById(R.id.eClr);
        iQnty   = (TextView) findViewById(R.id.eQnty);
        iCst    = (TextView) findViewById(R.id.eCost);
        iDsc    = (TextView) findViewById(R.id.eDsc);
        discard = (Button)   findViewById(R.id.dscrd);
        share   = (Button)   findViewById(R.id.email);

        db = openOrCreateDatabase("Shopping.db", MODE_PRIVATE, null);
        c = db.rawQuery("select * from SHOPPING_LIST", null);
        if(c.getCount()==0){

        }
        while(c.moveToNext()){
            s1= c.getString(0);
            s2= c.getString(1);
            s3= c.getString(2);
            s4= c.getString(3);
            s5= c.getString(4);
            s6= c.getString(5);
            s7= c.getString(6);
            s8= c.getString(7);
            s9= c.getString(8);
            s10=c.getString(9);
            s11=c.getString(10);
        }
        c.close();
        db.close();
        cat.setText(s1);
        prd.setText(s2);
        iName.setText(s3);
        iBrand.setText(s4);
        iAge.setText(s5);
        iColor.setText(s6);
        iQnty.setText(s7);
        iCst.setText(s8);
        iDsc.setText(s9);

    }
}

2 个答案:

答案 0 :(得分:0)

您可以使用各种库来生成PDF。这是链接:itext pdf generator is an open source library for android?

我个人在项目中使用过一次iText库。您可以选择其中任何一个。现在来看看你如何通过电子邮件分享。您可以通过以下链接获得答案:

Attaching file in email

Send a PDF file as Mail or offer app to directly view the file

如果您还有任何问题,请告诉我。

答案 1 :(得分:0)

创建pdf非常简单,但不知道在邮件中附加pdf文件

如果您使用的是Android-Studio,请首先下载droidText.0.2.jar并添加Gradle文件。然后为PDF编写此代码:

public void createPDF() {
    Document doc = new Document();


    try {
        path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ADUREC";

        File dir = new File(path);
        if (!dir.exists())
            dir.mkdirs();

        Log.d("PDFCreator", "PDF Path: " + path);

        //This is for random name 
        number = new ArrayList<Integer>();
        for (int i = 1; i <= 10; ++i) number.add(i);
        Collections.shuffle(number);

        File file = new File(dir, "Document" + number + ".pdf");
        FileOutputStream fOut = new FileOutputStream(file);
        PdfWriter.getInstance(doc, fOut);

        //open the document
        doc.open();

        Paragraph p1 = new Paragraph("TENANTS : " + tenants.getText().toString());
        Font paraFont = new Font(Font.COURIER);
        p1.setAlignment(Paragraph.ALIGN_CENTER);
        p1.setFont(paraFont);

        //add paragraph to document
        doc.add(p1);

        Paragraph p2 = new Paragraph("OFFFER NUMBER : " + offer_number.getText().toString());
        Font paraFont2 = new Font(Font.COURIER, 14.0f, Color.GREEN);
        p2.setAlignment(Paragraph.ALIGN_CENTER);
        p2.setFont(paraFont2);

        doc.add(p2);

        Paragraph p3 = new Paragraph("OFFFER NUMBER : " + offer_number.getText().toString());
        Font paraFont3 = new Font(Font.COURIER, 14.0f, Color.GREEN);
        p3.setAlignment(Paragraph.ALIGN_CENTER);
        p3.setFont(paraFont2);

        doc.add(p3);

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        Bitmap bitmap = BitmapFactory.decodeResource(getBaseContext().getResources(), R.drawable.logo);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        Image myImg = Image.getInstance(stream.toByteArray());
        myImg.setAlignment(Image.MIDDLE);

//            add image to document
        doc.add(myImg);

        //set footer
        Phrase footerText = new Phrase("ADUREC DOCUMENT");
        HeaderFooter pdfFooter = new HeaderFooter(footerText, true);
        doc.setFooter(pdfFooter);

//            Toast.makeText(getApplicationContext(), "success pdf", Toast
//                    .LENGTH_LONG).show();

    } catch (DocumentException de) {
        Log.e("PDFCreator", "DocumentException:" + de);
    } catch (IOException e) {
        Log.e("PDFCreator", "ioException:" + e);
    } finally {
        doc.close();
    }

}

然后调用此方法