【发布时间】: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 块中。并在返回前向用户敬酒。这是我第二次告诉你。调整你的代码!