【问题标题】:Try to send a csv file but the permission fails尝试发送 csv 文件但权限失败
【发布时间】:2017-03-16 03:35:18
【问题描述】:

我正在尝试发送文件 (Intent.ACTION_SEND),但是当我使用例如 gmail 时,权限失败,并且 gmail 通知我附件文件的权限不允许发送。 这是我的代码:

private void LoadSendFile() {

    String temp = new String();
    File tempFile = new File(Environment.getExternalStorageDirectory() + File.separator + "Datos" + ext);

    try {
        FileOutputStream fos = new FileOutputStream(tempFile);

        for (ActivityMat data : matriculas) {
            if (!data.getViajesConductor().isEmpty()) {
                for (String date : data.getViajesConductor()) {
                    temp += data.getName() + ": " + date + "\n";
                    fos.write(temp.getBytes());
                    Log.e("Write File: ", temp);
                }
            }
        }

        fos.close();

    } catch (FileNotFoundException e){
        Log.e("Creando fichero: ", e.getMessage(), e);
        Toast toast = Toast.makeText(MainActivity.this, "File not found ...", Toast.LENGTH_SHORT);
        toast.show();
        return;

    } catch (IOException e) {
        Log.e("Escribiendo fichero: ", e.getMessage(), e);
        Toast toast = Toast.makeText(MainActivity.this, "Can not build the file ...", Toast.LENGTH_SHORT);
        toast.show();
        return;
    }

    Intent sendIntent = new Intent(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(tempFile));
    sendIntent.setType("application/csv");
    startActivity(sendIntent);
}

提前致谢!!!

(请原谅我的语言,我不是英语)

更新: 代码已更新。现在我有权限了,但是 gmail 通知我它不能发送一个空文件,但是我知道(因为我在写入文件(Datos.csv)时写了一条日志消息)我在文件中写入。任何想法?? 这是日志(我看到是权限问题):

E/Creando fichero:: /storage/emulated/0/Datos.csv: open failed: EACCES (Permission denied)
                java.io.FileNotFoundException: /storage/emulated/0/Datos.csv: open failed: EACCES (Permission denied)
 ....  //here the "at" statement

Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)

【问题讨论】:

  • Context.MODE_PRIVATE。试试Context.WORLD_READABLE
  • 你有两个 catch 块。它们被使用了吗?在 logcat 中查看。你应该显示一个 toast 告诉用户。然后返回。请调整您的代码并尝试。然后在此处修改代码以显示您所做的。
  • 哦抱歉,我没看到,我更新了。
  • 查看更新的答案。
  • but gmail notify me that it can't send an empty file。当您有异常时,您不应该调用该意图。将 return 语句放在这些 catch 块中。并在返回前向用户敬酒。这是我第二次告诉你。调整你的代码!

标签: java android sendfile


【解决方案1】:

getFilesDir() 是您应用的私有内存。电子邮件应用无法访问它。

尝试其他位置,例如 getExternalStorageDirectory()getExternalFilesDir()

此外,您需要在清单文件中请求WRITE_EXTERNAL_STORAGE 权限。

如果为 Android 6 及更高版本编译,则在运行时请求用户对此的许可。

【讨论】:

  • 我更新代码并解释新问题。非常感谢您的帮助。
  • 是的,我之前添加了权限,但问题仍然存在。
  • 您都阅读了吗?当 >= 6.0 时,您也应该询问 gor 运行时权限。你甚至没有反应。
  • 是的,我正在执行运行时请求。现在好了。非常感谢您的帮助和耐心。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-23
  • 2019-05-07
  • 2020-12-13
  • 1970-01-01
  • 2016-12-30
  • 2011-12-28
  • 1970-01-01
相关资源
最近更新 更多