val intent = Intent()
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.action = Intent.ACTION_SEND
intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION;
intent.flags = Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
val type: String = FileDeleteUtils.getMimeType(file)
val uri = if(Build.VERSION.SDK_INT >= 24){
FileProvider.getUriForFile(context, context.packageName + ".fileprovider", file)
}else {
Uri.fromFile(file)
}
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setDataAndType(uri,type)
context.startActivity(intent)
private static String getSuffix(File file) {
if (file == null || !file.exists() || file.isDirectory()) {
return null;
}
String fileName = file.getName();
if (fileName.equals("") || fileName.endsWith(".")) {
return null;
}
int index = fileName.lastIndexOf(".");
if (index != -1) {
return fileName.substring(index + 1).toLowerCase(Locale.US);
} else {
return null;
}
}
public static String getMimeType(File file){
String suffix = getSuffix(file);
if (suffix == null) {
return "file/*";
}
String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(suffix);
if (type != null || !type.isEmpty()) {
return type;
}
return "file/*";
}
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths"
/>
</provider>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external" path="."/>
</paths>