下面是我正在尝试记录日志并将日志的txt文件附加到指定gmail的代码。它不断给我许可拒绝附件。查看图像以了解错误。
[https://i.stack.imgur.com/vVd5K.png][https://i.stack.imgur.com/LnOFs.png] [https://i.stack.imgur.com/DIhgk.png]
以下是权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
tools:node="replace" />
从文件中发送邮件意图的代码。
Intent intent = new Intent(Intent.ACTION_SEND); // this will show no apps available
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"techsupport@someemail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "Drive Bug Report");
intent.putExtra(Intent.EXTRA_TEXT, createReportABugBody());
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setType("text/plain");
Uri fileUri = getSleepLogFileUri();
intent.putExtra(Intent.EXTRA_STREAM, fileUri);
Intent chooser=Intent.createChooser(intent, "Email via...");
startActivityForResult(chooser,0);
写入文件的另一个函数:
private String createReportABugBody() {
Drive driveSDK = DriveApplication.getDrive();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Diagnostics").append("\n");
try{
PackageInfo pInfo = getActivity().getPackageManager().getPackageInfo(getActivity().getPackageName(), 0);
stringBuilder.append("Version: ").append(pInfo.versionName)
.append("(").append(pInfo.versionCode).append(")").append("\n");
} catch (PackageManager.NameNotFoundException e){
e.printStackTrace();
}
stringBuilder.append("SDK Version: ").append(driveSDK.getVersion()).append("\n");
stringBuilder.append("Device Model: Brand: ").append(Build.MANUFACTURER).append(", Model: ")
.append(Build.MODEL).append(", Product: ").append(Build.PRODUCT).append("\n");
stringBuilder.append("Type: Android \n");
stringBuilder.append("OS: ").append(Build.VERSION.RELEASE).append("\n");
stringBuilder.append("RM25 version: ").append(driveSDK.getRm25Version()).append("\n");
stringBuilder.append("Microphone enabled: ").append(
ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED)
.append("\n");
stringBuilder.append("User ID: ").append(Preferences.getPreference(Constants.KEY_STRING_USER_EMAIL, getActivity(), ""));
stringBuilder.append("\n\n\nSTART LOGS:\n");
try {
File sleepLog = new File(getContext().getFilesDir(), mFileName);
// Uri uri = getUriForFile(getContext(),FILE_PROVDER_AUTHORITY ,sleepLog);
// intent.putExtra(Intent.EXTRA_STREAM, uri);
BufferedReader br = new BufferedReader(new FileReader(sleepLog));
String line;
while ((line = br.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append('\n');
}
br.close();
}
catch (IOException e) {
e.printStackTrace();
}
return stringBuilder.toString();
}
获取文件
private Uri getSleepLogFileUri() {
/*
change intent to SENDTO from SEND.
will get "permission denied" from OS when Gmail opens.
*/
Uri uri=null;
try {
File filePath = new File(DriveApplication.getApplicationInstance().getFilesDir(), "logs");
File file = new File(filePath, mFileName);
//File file = new File(getContext().getFilesDir(), mFileName);
uri= Uri.fromFile(file);////getUriForFile(getContext(), FILE_PROVDER_AUTHORITY, filelocation);//Uri.fromFile(sleepLogFile);//
}catch (Exception e){
e.printStackTrace();
}
return uri;
}
答案 0 :(得分:0)
首先,第三方应用无法访问您应用的getFilesDir()
。
其次,您的应用应该在Android 7.0+上使用FileUriExposedException
崩溃。
要解决这两个问题,请使用FileProvider
为您的文件投放,并使用FileProvider.getUriForFile()
在Uri
中使用EXTRA_STREAM
。