问题描述: 当在Android应用程序中使用Android FileProvider来共享文件时,通过Gmail或Google Drive分享文件失败。
解决方法:
...
provider_paths.xml内容示例:
File file = new File(getFilesDir(), "your_file_name");
file.setReadable(true, false);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
}
File file = new File(getFilesDir(), "your_file_name");
if (file.exists()) {
// 共享文件的代码
} else {
// 文件不存在的处理
}
File file = new File(getFilesDir(), "your_file_name");
Uri fileUri = FileProvider.getUriForFile(this, "com.your.package.name.fileprovider", file);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_STREAM, fileUri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(intent, "Share File"));
以上是一些可能导致Android FileProvider在使用Gmail和Google Drive共享文件时失败的常见问题解决方法。根据实际情况,你可能需要根据你的应用程序和共享的文件进行适当的调整。