Android附件不是通过电子邮件发送的

Android附件不是通过电子邮件发送的,android,android-intent,Android,Android Intent,我正在开发android应用程序,在某个时候我想允许用户通过电子邮件发送一个.csv文件。 我在网上看过很多教程,它们都有相同的代码,我试过很多次了。问题是,发送电子邮件活动开始,附件出现在电子邮件中,但当它被发送时,文件或将变空到目标,或根本不出现在目标中 下面是一些代码: File file = null; File dir = ProjectViewerResume.this.getCacheDir(); if (dir.canWrite()){ file = new File(d

我正在开发android应用程序,在某个时候我想允许用户通过电子邮件发送一个.csv文件。 我在网上看过很多教程,它们都有相同的代码,我试过很多次了。问题是,发送电子邮件活动开始,附件出现在电子邮件中,但当它被发送时,文件或将变空到目标,或根本不出现在目标中

下面是一些代码:

File file = null;
File dir = ProjectViewerResume.this.getCacheDir();
if (dir.canWrite()){
    file = new File(dir, ProjectViewerResume.this.mProject.getName()+".csv");
    FileOutputStream out = new FileOutputStream(file);
    out.write(csv.toString().getBytes());
    out.close();
}
Uri u1 = null;

u1 = Uri.fromFile(file);

Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Project Resume: "+
                ProjectViewerResume.this.mProject.getName());
sendIntent.putExtra(Intent.EXTRA_STREAM, u1);
sendIntent.setType("text/csv");
startActivity(sendIntent);
csv变量具有csv格式的文件内容。 我省略了try..catch,因此代码看起来更干净

Thks提前

你可以试试这个

Intent sendIntent = new Intent(Intent.ACTION_SEND);
    sendIntent.setType("text/plain");
    sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject of Email");
    sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("/mnt/sdcard/file.csv"));
    sendIntent.putExtra(Intent.EXTRA_TEXT, "Enjoy the mail");
    startActivity(Intent.createChooser(sendIntent, "Email:"));    
如果附加文件时出现问题,请尝试以下路径

sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/file.csv"));

我也有同样的问题。因此,为了避免重复同一篇文章,我想向您展示我的代码,它与您的代码略有不同,但仍然发送一封带有0 KB附件的电子邮件。
我将2条消息打印到日志:
  • 打印路径(我编辑了包名)
  • 文件的大小不是0KB

    05-11 11:32:58.133: W/Log Viewer(432): Path is /data/data/<package>/files/LogCat.log.gzip
    05-11 11:32:58.192: W/Log Viewer(432): The file is 504 bytes
    
    我尝试过的事情:
  • 我通过解析创建了uri(参见注释行
  • 我还将数据放在电子邮件正文中,以检查它是否存在-->它到达电子邮件中
  • 我还尝试将gziped数据放入电子邮件正文中,以检查它是否存在-->它已到达电子邮件
    编辑:
  • 试图从/mnt/system/app/CustomLocale.apk
    23745字节发送模拟器中预先存在的文件 uriLog.getPath()中的路径已打印为/apk/CustomLocale.apk
    ,邮件到达时仍然带有名为CustomLocale.apk的附件和0KB大小

    我觉得唯一失败的是putextra(intent.EXTRA_STREAM,uri)是基于

    public Intent putExtra (String name, Parcelable value) 
    

    URI实现parcelable接口。但是出于某种原因,我将调查,可能对writeToParcel(Parcel out,int flags)的调用没有写入文件数据;这就是为什么附件中的文件数据是0KB。

    此代码现在对我有效。
    使用createTempFile而不是openFileOutput。我仍然不知道前一种方法有什么问题。
    它附加了一个名为LogCat.log.gzip的文件,该文件这次不是空的,它包含我输入的数据

    public void run(){
    Uri uriLog=null;
    试一试{
    File gzipFile=File.createTempFile(“LogCat.log”,“.gzip”);
    FileOutputStream out=新的FileOutputStream(gzip文件);
    write(gzip(dump().getBytes());
    out.close();
    uriLog=Uri.fromFile(gzip文件);
    }
    捕获(IOE异常){
    e、 printStackTrace();
    }
    Intent emailIntent=新的Intent(android.content.Intent.ACTION\u SEND);
    emailIntent.setType(“文本/普通”);
    emailIntent.putExtra(android.content.Intent.EXTRA_主题,“android日志”);
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,dump());
    emailIntent.putExtra(android.content.Intent.EXTRA_STREAM,uriLog);
    试一试{
    String str=getResources().getString(R.String.send\u提示符);
    Intent chooser=Intent.createChooser(emailIntent,str);
    星触觉(选择器);
    }
    捕获(例外e){
    e、 printStackTrace();
    }
    }
    }).start();
    
    别忘了将这一行添加到您的清单中:

    
    
    不要硬连线路径。使用
    Environment.getExternalStorageDirectory()
    标识外部存储的根目录。我已经测试了上述代码,但仍然得到一个空的附件…有什么想法吗?上述代码对您有用吗?您需要记住,电子邮件应用程序无法访问应用程序个人存储中的文件,要附加到电子邮件,您需要将文件移动到电子邮件应用程序可以访问的位置,如外部存储。我认为问题在于,电子邮件客户端无法访问(出于安全原因)文件所在的路径/data/data//files/LogCat.log.gzip。只有你自己的应用程序才能做到这一点。尝试将其存储在电子邮件客户端有权访问的其他位置。
    public Intent putExtra (String name, Parcelable value)