【问题标题】:Trying to send a created text file as an email attachment - from a default folder尝试将创建的文本文件作为电子邮件附件发送 - 从默认文件夹
【发布时间】:2011-12-05 12:02:32
【问题描述】:

我正在尝试一些简单的事情,例如创建一个文本文件,然后将其作为附件发送。虽然如果我使用 sdcard 可以正常工作,但我不知道将它放在“标准数据文件夹”的哪个位置,所以我的应用实际上适用于没有 sdcard 的每个人(并且该文件有些不可见)

虽然此代码有效,但我将问题放在 * 评论中。

创建文件时:

String FILENAME = "myFile.txt";
    String string = "just something";

    // create a File object for the parent directory
    File myDirectory = new File("/sdcard/myDir/");  // ******** <- what do I have to put HERE for standard data folder????
    // have the object build the directory structure, if needed.
    myDirectory.mkdirs();
    // create a File object for the output file
    File outputFile = new File(myDirectory, FILENAME);
    // now attach the OutputStream to the file object, instead of a String representation
    FileOutputStream fos = null;
   //always have to put try/catch around the code - why? I don't know
    try {
        fos = new FileOutputStream(outputFile);
    } catch (FileNotFoundException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }

     //again have to put try/catch around it - otherwise compiler complains
    try {
        fos.write(string.getBytes());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }

发送文件时:

public void doSendFile() {
    String fileName = "/sdcard/myDir/myFile.txt"; // ******** <- what do I have to put HERE for standard data folder????
    Intent i = new Intent(Intent.ACTION_SEND);
    try {
        mainDataManager.logFileHandle.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    i.setType("text/plain");
    i.putExtra(Intent.EXTRA_EMAIL, new String[] { "to@someone.com" });
    i.putExtra(Intent.EXTRA_SUBJECT, "subject");
    i.putExtra(Intent.EXTRA_TEXT, "text");
    Log.i(getClass().getSimpleName(),
        "logFile=" + Uri.parse("file://" + fileName));
    i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + fileName));

    try {
        startActivity(Intent.createChooser(i, "Send mail..."));
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(getBaseContext(),
            "There are no email clients installed.", Toast.LENGTH_SHORT)
            .show();
    }

    }

我发现该文件在创建时似乎存储在“data/data/com.xxx.xxxx/databases/myFile.txt”中。 但是当我在附件中使用它时,什么都没有发送。

所以基本上我只需要知道如何将文件存储在本地内存中,然后从那里发送。因为不是每个人都可能有一个外部存储 SD 卡 - 我想。

谢谢!

【问题讨论】:

    标签: android email email-attachments


    【解决方案1】:

    这是因为 SD 卡没有受到保护,因此您可以像以前一样以标准的 java 方式写入那里。

    为了写入 Android 文件系统,您不能使用它,因为每个文件都使用 App 密钥进行保护,以避免其他应用使用它。

    您应该使用 openFileOutput() 了解更多信息,请参阅本教程:

    http://www.anddev.org/working_with_files-t115.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-11
      • 2011-12-20
      • 2013-03-19
      • 2021-02-04
      • 2013-07-11
      • 2015-12-21
      • 2013-07-26
      • 2021-07-18
      相关资源
      最近更新 更多