【问题标题】:Email apps cannot read the file I'm trying to attach电子邮件应用程序无法读取我尝试附加的文件
【发布时间】:2013-09-30 17:44:51
【问题描述】:

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

这是我的代码。谢谢!!

AndroidManifest 内部 使用权限 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:"));`

【问题讨论】:

  • 请解释您所说的“无法读取文件”是什么意思。您收到了哪些具体的错误消息?另外,当您不使用ContentProvider 时,为什么还要包括FLAG_GRANT_READ_URI_PERMISSION?您可能还希望考虑使用sync()android-developers.blogspot.com/2010/12/saving-data-safely.html
  • 您好,感谢您的回答。我来自西班牙,消息是西班牙语的。我将尝试翻译它们。 Gmail 应用程序显示:“Se ha detenido la aplicación”是什么意思“应用程序已停止”。当我按下按钮发送它但文件在那里作为附件时会发生这种情况。雅虎应用程序说:“Acceso de lectura denegado。El archivo seleccionado no se puede leer”意思是“读取访问被拒绝。无法读取所选文件”。 FLAG_GRANT_....?我只是在尝试。我将阅读有关 sync() 的链接。谢谢。

标签: android file email


【解决方案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())。

【讨论】:

    【解决方案2】:

    最后,成功了!!!我真的不知道发生了什么。我在没有安装应用程序的情况下尝试将我的手机用作带有 eclipse 的模拟器,并且异常的 e.getMessage() 是:“/storage/sdcard/Torno/Torno.xml: open failed: ENOENT (No such file or directory)”

    最后,我想安装该应用程序并且它有效!这是当前代码:

    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;
    }
    

    谢谢!!!

    【讨论】:

      猜你喜欢
      • 2013-04-08
      • 1970-01-01
      • 1970-01-01
      • 2017-01-31
      • 1970-01-01
      • 1970-01-01
      • 2010-10-09
      • 1970-01-01
      • 2012-03-07
      相关资源
      最近更新 更多