android导出到csv并作为电子邮件附件发送

Kri*_*dra 19 csv email android export attachment

我在这个网站上看到多个线程讨论在android中发送带附件的电子邮件.我想讨论的每一个方法在这里,这里这里.

我正在通过代码创建一个csv文件并将此文件保存到android内部存储.然后我想在电子邮件中将此文件作为附件发送.嗯,电子邮件正在发送,我收到它没有附件.这就是我所做的.

String columnString         =   "\"Person\",\"Gender\",\"Street1\",\"PostOfice\",\"Age\"";
String dataString           =   "\"" + currentUser.userName +"\",\"" + currentUser.gender + "\",\"" + currentUser.street1 + "\",\"" + currentUser.poNumber.toString() + "\",\"" + currentUser.age.toString() + "\"";
String combinedString       =   columnString + "\n" + dataString;
File file                   =   new File(this.getCacheDir()+ File.separator + "Data.csv");
try {
    FileOutputStream out    =   new FileOutputStream(file);
    out.write(combinedString.getBytes());
    out.close();
} catch (IOException e) {
    Log.e("BROKEN", "Could not write file " + e.getMessage());
}   
Uri u1                      =   Uri.fromFile(file);

Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Person Details");
sendIntent.putExtra(Intent.EXTRA_STREAM, u1);
sendIntent.setType("text/richtext");
startActivity(sendIntent);
Run Code Online (Sandbox Code Playgroud)

我尝试将mime设置更改为"text/html"和"text/richtext"等.但还没有运气.谁能告诉我我做错了什么?

Kri*_*dra 50

感谢所有试图提供帮助的人.花了一整天的时间后,我发送了一封来自我的app附带附件的电子邮件.这是工作代码..

String columnString =   "\"PersonName\",\"Gender\",\"Street1\",\"postOffice\",\"Age\"";
String dataString   =   "\"" + currentUser.userName +"\",\"" + currentUser.gender + "\",\"" + currentUser.street1 + "\",\"" + currentUser.postOFfice.toString()+ "\",\"" + currentUser.age.toString() + "\"";
String combinedString = columnString + "\n" + dataString;

File file   = null;
File root   = Environment.getExternalStorageDirectory();
if (root.canWrite()){
    File dir    =   new File (root.getAbsolutePath() + "/PersonData");
     dir.mkdirs();
     file   =   new File(dir, "Data.csv");
     FileOutputStream out   =   null;
    try {
        out = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            out.write(combinedString.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Uri u1  =   null;
u1  =   Uri.fromFile(file);

Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Person Details");
sendIntent.putExtra(Intent.EXTRA_STREAM, u1);
sendIntent.setType("text/html");
startActivity(sendIntent);
Run Code Online (Sandbox Code Playgroud)

此外,如果您已将手机SDCard安装在机器中,则此代码无法使用.只有一个人可以同时访问SDCard.所以在那种情况下从计算机上卸下你的SD卡并试试......感谢那些在这里回答的人.还要确保你已经购买了在你的清单文件中写入外部存储的权限......

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
Run Code Online (Sandbox Code Playgroud)

希望它有所帮助...感谢所有试图帮助的人..


归档时间:

查看次数:

32089 次

最近记录:

7 年,8 月 前