0

我正在尝试在 android 中创建一个 pdf 文档,以便使用 android 中的 Intent 通过电子邮件发送。请任何人都可以帮助解决这个问题。我相信我的问题领域是意图,但我不完全确定。以下是我到目前为止的代码,谢谢:

    private String cmail = "myselftest123@gmail.com";

       @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_apply);

    firstnameTV = (TextView)findViewById(R.id.texViewFName);
    firstnameET = (EditText)findViewById(R.id.editTextFName);
    lastnameET = (EditText)findViewById(R.id.editTextLName);
    lastnameTV = (TextView)findViewById(R.id.texViewLName);
    address1TV = (TextView)findViewById(R.id.texViewAddress1);
    address1ET = (EditText)findViewById(R.id.editTextAddress1);
    address2TV = (TextView)findViewById(R.id.texViewAddress2);
    address2ET = (EditText)findViewById(R.id.editTextAddress2);
    address3TV = (TextView)findViewById(R.id.texViewAddress3);
    address3ET = (EditText)findViewById(R.id.editTextAddress3);
    address4TV = (TextView)findViewById(R.id.texViewAddress4);
    address4ET = (EditText)findViewById(R.id.editTextAddress4);
    emailTV = (TextView)findViewById(R.id.texViewEmail);
    emailET = (EditText)findViewById(R.id.editTextEmail);
    dobTV = (TextView)findViewById(R.id.texViewDOB);
    dobET = (EditText)findViewById(R.id.editTextDOB);
    submitBtn = (Button)findViewById(R.id.btnSubmit);

    submitBtn.setOnClickListener(this);
    entries();
    addSaveData(document);
    try {
        addPage(document);
    } catch (DocumentException e) {

        e.printStackTrace();
    }

}

public void entries()
{
    fName = firstnameET.getText().toString();
    lName = lastnameET.getText().toString();
    Dob = dobET.getText().toString();
    address1 = address1ET.getText().toString();
    address2 = address2ET.getText().toString();
    address3 = address3ET.getText().toString();
    address4 = address4ET.getText().toString();
    uEmail = emailET.getText().toString();
}

public void addSaveData(Document document) 
{
    document.addTitle("Application Form");
    document.addSubject("Using iText");
    document.addKeywords("App, Form");
    document.addAuthor("Myself");
    document.addCreator("Myself");
 }
   private void addLineSpace(Paragraph paragraph, int number) 
   {
    for (int i = 0; i < number; i++) {
      paragraph.add(new Paragraph(" "));
     }
    }
private void addPage(Document document) throws DocumentException
   {
    Paragraph preface = new Paragraph();
    addLineSpace(preface, 1);
    preface.add(new Paragraph("Application Form", titleFont));

    addLineSpace(preface, 1);
    preface.add(new Paragraph("Form" , subTitleFont));

    addLineSpace(preface, 2);
    preface.add(new Paragraph("First name: " + fName, normalFont) );

    addLineSpace(preface, 1);
    preface.add(new Paragraph("Last name: " + lName, normalFont) );

    addLineSpace(preface, 1);
    preface.add(new Paragraph("Date of Birth: " + Dob, normalFont) );

    addLineSpace(preface, 1);
    preface.add(new Paragraph("Email " + uEmail, normalFont) );

    addLineSpace(preface, 1);
    preface.add(new Paragraph("Address 1 " + address1, normalFont) );

    addLineSpace(preface, 1);
    preface.add(new Paragraph("Address 2 " + address2, normalFont) );

    addLineSpace(preface, 1);
    preface.add(new Paragraph("Address 3 " + address3, normalFont) );

    addLineSpace(preface, 1);
    preface.add(new Paragraph("Address 4 " + address4, normalFont) );

    document.add(preface);
    // For new page
    //document.newPage();
  }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.apply, menu);
    return true;
}

@Override  //problem!!!
public void onClick(View v) {

     Intent email = new Intent(Intent.ACTION_SEND);
     email.setType("document/pdf");
     email.putExtra(Intent.EXTRA_EMAIL, cmail);
     email.putExtra(Intent.EXTRA_STREAM, document);
     startActivity(email);  

}
4

1 回答 1

1

我正在对我的应用程序Pic4Share做类似的事情。以 PDF 格式创建相册后,用户可以将其作为电子邮件附件进行共享。这是打开选择器以与已附加的文件共享并预加载一些消息的代码:

//create the send intent
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);

// File path
String fileName = "file://" + android.os.Environment.getExternalStorageDirectory() + "/" + getResources().getString(R.string.folder_name) + "/" + albumTitle + ".pdf";
Uri pdfUri = Uri.parse(fileName);

//set the type
shareIntent.setType("application/pdf");

//add a subject
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, albumTitle);

//build the body of the message to be shared
String shareMessage = getResources().getString(R.string.label_share_message);

//add the message
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareMessage);

//add the attachment
shareIntent.putExtra(Intent.EXTRA_STREAM, pdfUri);

//start the chooser for sharing
startActivity(Intent.createChooser(shareIntent, getResources().getString(R.string.label_chooser_title)));

也许你可以把它作为一个起点。

于 2013-07-07T03:15:07.763 回答