【问题标题】:Attaching a PDF to an Email from Android App - File Size is Zero从 Android 应用程序将 PDF 附加到电子邮件 - 文件大小为零
【发布时间】:2012-05-21 13:29:51
【问题描述】:

我正在尝试将名为 download.pdf 的 PDF 文件附加到我的 Android 应用程序中的电子邮件中。我先将文件复制到 SDCard,然后将其附加到电子邮件中。

我不相关,但我正在 Galaxy Tab 设备上进行测试。外部存储路径返回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 字节。谁能发现可能出了什么问题?

编辑

如果我查看设置,我可以看到文件已保存到设备上,因此这一定是我如何将文件附加到电子邮件的问题。在我看到的错误日志中:

gMail Attachment URI: file:///mnt/sdcard/download.pdf
gMail type: application/pdf
gmail name: download.pdf
gmail size: 0

编辑

想知道这是否是银河选项卡上的错误?如果我通过 pdf 查看器(从我的应用程序)打开文件,然后尝试附加到 gmail 电子邮件,大小再次为 0。任何人都可以验证吗?

谢谢。

【问题讨论】:

  • 您的应用是否有权访问 SD 卡?
  • 是 - 在清单中 -
  • 您是否检查了您使用copyAssets() 方法复制的sdcard 中的pdf 文件大小。
  • 是的 - 尺寸合适。
  • 您找到解决此问题的方法了吗?

标签: java android email pdf email-attachments


【解决方案1】:
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:"));

【讨论】:

    【解决方案2】:

    如果您的 download.pdf 文件存在于 SDCard 中。所以问题应该是从文件中获取 Uri。试试这个,它对我有用。

    Uri uri = Uri.fromFile(new File("/sdcard/", "download.pdf"));
    

    【讨论】:

    【解决方案3】:

    同样的问题也发生在我身上。我已经使用示例中的一些方法清除了这一点。我已经answered 提出了与您的查询类似的问题。也许这对你有帮助。

    【讨论】:

      【解决方案4】:
      File externalStorage = Environment.getExternalStorageDirectory();    
      String PDFpath = externalStorage.toString();
      String pdfpath =path.replace("/mnt","");
      Uri uri = Uri.parse(new File("file://" + pdfpath));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多