将PDF附加到Android应用的电子邮件 - 文件大小为零 [英] Attaching a PDF to an Email from Android App - File Size is Zero
问题描述
我正在尝试将一个名为download.pdf的PDF文件附加到我的Android应用程序的电子邮件中。我将文件首先复制到SDCard,然后附上电子邮件。
I am trying to attach a PDF file called download.pdf to an email in my Android App. I am copying the file first to the SDCard and then attaching it the email.
我不是相关的,但我正在一个星系选项卡设备上进行测试。外部存储路径返回mnt / sdcard /
I'm not if relevant, but I am testing on a galaxy tab device. The external storage path returns mnt/sdcard/
我的代码如下:
public void sendemail() throws IOException {
CopyAssets();
String emailAddress[] = {""};
File externalStorage = Environment.getExternalStorageDirectory();
Uri uri = Uri.fromFile(new File(externalStorage.getAbsolutePath() + "/" + "download.pdf"));
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, emailAddress);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Text");
emailIntent.setType("application/pdf");
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(emailIntent, "Send email using:"));
}
public void CopyAssets() {
AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list("");
} catch (IOException e) {
Log.e("tag", e.getMessage());
}
for(String filename : files) {
InputStream in = null;
OutputStream out = null;
if (filename.equals("download.pdf")) {
try {
System.out.println("Filename is " + filename);
in = assetManager.open(filename);
File externalStorage = Environment.getExternalStorageDirectory();
out = new FileOutputStream(externalStorage.getAbsolutePath() + "/" + filename);
System.out.println("Loacation is" + out);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch(Exception e) {
Log.e("tag", e.getMessage());
}
}
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}
}
问题是文件附加的是大小为0字节。任何人可能会发现可能是错误的?
The problem is that the file that is attached is 0 bytes in size. Can anyone spot what might be wrong ?
编辑
我可以看到如果我查看设置,文件已被保存到设备上,因此这个文件必须是将文件附加到电子邮件中的问题。在错误日志中,我看到:
I can see that the file has been saved onto the device if I look in settings, therefore this must be a problem around how I am attaching the file to the email. In the error log I am seeing :
gMail Attachment URI: file:///mnt/sdcard/download.pdf
gMail type: application/pdf
gmail name: download.pdf
gmail size: 0
编辑
想知道这是星系选项卡上的错误吗?如果我通过pdf查看器(从我的应用程序)打开文件,然后尝试附加到Gmail邮件,大小再次为0.任何人都可以验证?
Wondering if this is a bug on the galaxy tab ? If I open the file via a pdf viewer (from my app) then try to attach to a gmail email, the size is again 0. Can anyone verify ?
谢谢
推荐答案
String[] mailto = {""};
Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/CALC/REPORTS/",pdfname ));
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, mailto);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Calc PDF Report");
emailIntent.putExtra(Intent.EXTRA_TEXT, ViewAllAccountFragment.selectac+" PDF Report");
emailIntent.setType("application/pdf");
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(emailIntent, "Send email using:"));
这篇关于将PDF附加到Android应用的电子邮件 - 文件大小为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!