尝试发送 csv 文件但权限失败
Try to send a csv file but the permission fails
我正在尝试发送文件 (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)
getFilesDir()
是您应用程序的私有内存。电子邮件应用程序无权访问它。
尝试其他位置,例如 getExternalStorageDirectory()
或 getExternalFilesDir()
。
此外,您需要在清单文件中请求 WRITE_EXTERNAL_STORAGE
权限。
如果针对 Android 6 及更高版本进行编译,则在运行时请求用户的许可。
我正在尝试发送文件 (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)
getFilesDir()
是您应用程序的私有内存。电子邮件应用程序无权访问它。
尝试其他位置,例如 getExternalStorageDirectory()
或 getExternalFilesDir()
。
此外,您需要在清单文件中请求 WRITE_EXTERNAL_STORAGE
权限。
如果针对 Android 6 及更高版本进行编译,则在运行时请求用户的许可。