我正在尝试在android中创建一个pdf文档,使用Intent在android中通过电子邮件发送。请任何人都可以帮忙。我相信我的问题区域是意图,但我不完全确定。以下是我的代码,谢谢:
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);
}
答案 0 :(得分: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)));
也许你可以用它作为起点。