电子邮件应用无法读取我尝试附加的文件

时间:2013-09-30 17:44:52

标签: android file email

我将文件写入SD。我知道文件没问题,因为使用ASTRO应用程序或Gmail应用程序或雅虎应用程序,我可以看到它,我也可以从它们附加它,但当我尝试从我的应用程序附加文件时,事情是完全不同的。当我选择gmail或雅虎应用程序时,他们无法读取该文件。但是,如果我选择默认应用程序,则会使用附加的文件正确发送电子邮件。

这是我的代码。谢谢!

AndroidManifest内部    uses-permission android:name =“android.permission.WRITE_EXTERNAL_STORAGE”

我在哪里制作文件

File ruta_sd = Environment.getExternalStorageDirectory();
File f;

// Creo la carpeta;
File folder = new File(ruta_sd.getAbsolutePath() + "/Torno");
folder.mkdirs();

f = new File(ruta_sd.getAbsolutePath() + "/Torno/","Torno.xml");

// Just trying
f.canRead();

OutputStreamWriter fout = new OutputStreamWriter(new FileOutputStream(f));
fout.write(c); <-- c is a String in xml format

// Just trying
fout.flush();

fout.close(); 

现在,我试图附上它

String ruta = Environment.getExternalStorageDirectory().getPath() + "/Torno/Torno.xml";
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType(getMimeType(ruta));
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(ruta));
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "el tema");
sendIntent.putExtra(Intent.EXTRA_TEXT, "el cuerpo del mensaje");

// Just trying
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

startActivity(Intent.createChooser(sendIntent, "Title:"));`

2 个答案:

答案 0 :(得分:1)

据推测,你的道路有问题。变化:

String ruta = Environment.getExternalStorageDirectory().getPath() + "/Torno/Torno.xml";
...
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(ruta));

为:

File ruta = new File(Environment.getExternalStorageDirectory(), "/Torno/Torno.xml");
...
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(ruta));

并查看是否有帮助(同时删除FLAG_GRANT_READ_URI_PERMISSION,并尝试sync())。

答案 1 :(得分:0)

最后,它工作了!!!我真的不知道发生了什么。我试图用我的手机作为eclipse的模拟器而不安装应用程序,异常的e.getMessage()是:“/ storage / sdcard / Torno / Torno.xml:open failed:ENOENT(没有这样的文件或目录)”

最后,我想安装应用程序,它工作了!!这是当前的代码:

btnAceptar.setOnClickListener(new OnClickListener() {

                @Override 
             public void onClick(View arg0) { 
                     // TODO Auto-generated method stub 
                     String contenido="";

                     File fichero = null;

                   if (rdXML.isChecked()){ 
                          contenido = creaFichero(1,Integer.parseInt("" + txtAno.getText()),Integer.parseInt("" + txtMes.getText()));
                          fichero = grabarFichero(contenido, "Torno.xml");
                   } 
                   else{ 
                          contenido = creaFichero(2,Integer.parseInt("" + txtAno.getText()),Integer.parseInt("" + txtMes.getText()));
                          fichero = grabarFichero(contenido, "Torno.txt");
                   } 

                   if (fichero==null){ 
                           Toast toast = Toast.makeText(getApplicationContext(),"No hay datos para ese mes !!", Toast.LENGTH_LONG);
                           toast.show(); 
                   } 
                   else{
                       Intent sendIntent = new Intent(Intent.ACTION_SEND);
                       sendIntent.setType(getMimeType(fichero.getAbsolutePath()));

                       sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(fichero));
                       sendIntent.putExtra(Intent.EXTRA_SUBJECT, "el tema");
                       sendIntent.putExtra(Intent.EXTRA_TEXT, "el cuerpo del mensaje");
                       startActivity(Intent.createChooser(sendIntent, "Title:"));
                   } 
           } 
   });

public static String getMimeType(String url){
    String type = null;
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    if (extension != null) {
        MimeTypeMap mime = MimeTypeMap.getSingleton();
        type = mime.getMimeTypeFromExtension(extension);
    }
    return type;
}

private File grabarFichero(String c,String n){

    File file = null;

    try {
        // Creo la carpeta;
        File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Torno");
        folder.mkdirs();

        file = new File(Environment.getExternalStorageDirectory() + "/Torno", n);
        OutputStreamWriter outw = new OutputStreamWriter(new FileOutputStream(file));
        outw.write(c);
        outw.close();
    }
    catch (Exception e) {} 

    return file;
}

感谢!!!