Android发送xml文件不发送附件 [英] Android send xml file doesn't send attachment

查看:129
本文介绍了Android发送xml文件不发送附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了几个例子,但仍然没有找到原因,当我编辑邮件时,我看到.xml附件,但是当我收到没有附件的时候!



这是我的代码

 文件f =新建文件(data / data / xxx / files / xxx.xml ); 
Boolean b1 = f.exists();
Boolean b2 = f.canRead();
if(b1&& b2){
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType(text / plain);
sendIntent.putExtra(Intent.EXTRA_EMAIL,);
sendIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse(file://+
f.getAbsolutePath()));
sendIntent.putExtra(Intent.EXTRA_SUBJECT,XXX);
sendIntent.putExtra(Intent.EXTRA_TEXT,R.string.mail_body);
startActivity(Intent.createChooser(sendIntent,Email:));
} else {
...

啊,只有一个细节..当我选择发送的应用程序没有主题或正文,即使我写了putExtra(Intent.EXTRA_SUBJECT)和putExtra(Intent.EXTRA_TEXT),但这是一个细节...



编辑:我刚刚调试了我的意图:它在流的价值中说NOT CACHED,如何解决?



解决方案

您无法直接从内部存储中附加文件以达到某种安全目的,因此首先必须将该文件从内部副本复制到外部目录,然后再将该文件从该外部存储文件中删除,如果您希望可以使用onActivityResult()方法从外部存储中删除该文件



这是一个代码:

 私有文件copyFileToExternal(String fileName ){
文件文件= null;
String newPath = Environment.getExternalStorageState()+/ folderName /;
try {
File f = new File(newPath);
f.mkdirs();
FileInputStream fin = openFileInput(fileName);
FileOutputStream fos = new FileOutputStream(newPath + fileName);
byte [] buffer = new byte [1024];
int len1 = 0;
while((len1 = fin.read(buffer))!= -1){
fos.write(buffer,0,len1);
}
fin.close();
fos.close();
file = new File(newPath + fileName);
if(file.exists())
返回文件;
} catch(异常e){

}
返回null;
}

电子邮件的方法:



文件文件= new File(Environment.getExternalStorageState()+/ folderName /+ fileName +.xml );
Uri path = Uri.fromFile(file);
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType(application / octet-stream);
intent.putExtra(android.content.Intent.EXTRA_SUBJECT,subject);
String to [] = {email};
intent.putExtra(Intent.EXTRA_EMAIL,to);
intent.putExtra(Intent.EXTRA_TEXT,message);
intent.putExtra(Intent.EXTRA_STREAM,path);
startActivityForResult(Intent.createChooser(intent,Send mail ...),
1222);
}

然后

  protected void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode,resultCode,data);
if(requestCode == 1222){
文件文件= new File(Environment.getExternalStorageState()+/ folderName /+ fileName +.xml);
file.delete();
}
}

调用这样的方法:

  copyFileToExternal(filename +.xml); 
sendEmail(EmailId);


I've seen several exemples but still don't get why, when I'm editing the mail I see the .xml attached but when I receive ther's no attachment!

Here is my code

File f = new File("data/data/xxx/files/xxx.xml");
                    Boolean b1 = f.exists();
                    Boolean b2 = f.canRead();
                       if (b1 && b2) {
                          Intent sendIntent = new Intent(Intent.ACTION_SEND);
                          sendIntent.setType("text/plain");
                          sendIntent.putExtra(Intent.EXTRA_EMAIL, "");
                          sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" +
                             f.getAbsolutePath()));
                          sendIntent.putExtra(Intent.EXTRA_SUBJECT, "XXX");
                          sendIntent.putExtra(Intent.EXTRA_TEXT, R.string.mail_body);
                          startActivity(Intent.createChooser(sendIntent, "Email:"));
                       } else {
...

Ah, only a detail...when I choose the app to send there is no subject or body, even if I wrote putExtra(Intent.EXTRA_SUBJECT) and putExtra(Intent.EXTRA_TEXT), but that's a detail...

Edit: I just debuged my intent: it says "NOT CACHED" in value of the stream, how to solve it?

解决方案

You can't attach a file from internal storage directly for some security purpose, hence first you have to copy that file from internal to external directory and then mail after that if you want you can delete that file from external storage in onActivityResult() method.

Here's a code :

private  File copyFileToExternal(String fileName) {
        File file = null;
        String newPath = Environment.getExternalStorageState()+"/folderName/";
        try {
            File f = new File(newPath);
            f.mkdirs();
            FileInputStream fin = openFileInput(fileName);
            FileOutputStream fos = new FileOutputStream(newPath + fileName);
            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = fin.read(buffer)) != -1) {
                fos.write(buffer, 0, len1);
            }
            fin.close();
            fos.close();
            file = new File(newPath + fileName);
            if (file.exists())
                return file;
        } catch (Exception e) {

        }
        return null;
    }

Method to Email:

private void sendEmail(String email) {

        File file = new File(Environment.getExternalStorageState()+"/folderName/" + fileName+ ".xml");
        Uri path = Uri.fromFile(file);
        Intent intent = new Intent(android.content.Intent.ACTION_SEND);
        intent.setType("application/octet-stream");
        intent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
        String to[] = { email };
        intent.putExtra(Intent.EXTRA_EMAIL, to);
        intent.putExtra(Intent.EXTRA_TEXT, message);
        intent.putExtra(Intent.EXTRA_STREAM, path);
        startActivityForResult(Intent.createChooser(intent, "Send mail..."),
                1222);
    }

and then

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1222) {
            File file = new File(Environment.getExternalStorageState()+"/folderName/" + fileName+ ".xml");
            file.delete();
}
}

Call this method like this:

 copyFileToExternal(filename + ".xml");
 sendEmail(EmailId);

这篇关于Android发送xml文件不发送附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆