0

我将文件写入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:"));`
4

2 回答 2

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())。

于 2013-10-02T18:04:18.987 回答
0

最后,成功了!!!我真的不知道发生了什么。我在没有安装应用程序的情况下尝试将我的手机用作带有 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-10-03T12:53:31.987 回答