
在 res / xml / provider_paths.xml中* 创建一个xml文件 *
<?xml version="1.0" encoding="utf-8"?><paths xmlns:android="http://schemas.android.com/apk/res/android"> <!-- name is the file name path is the root of external storage, it means here: Environment.getExternalStorageDirectory() --> <external-path name="scale" path="."/> <!-- another example: Environment.getExternalStorageDirectory() + File.separator + "temps" + "myFile.pdf" --> <external-path name="myFile" path="temps"/></paths>
在 清单* 中的应用程序标记中添加提供程序 *
<!--android:name="android.support.v4.content.FileProvider"--><provider android:name="androidx.core.content.FileProvider" android:authorities="your.application.package.fileprovider" android:grantUriPermissions="true" android:exported="false"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" /></provider>
最后将代码更改为此:
public static void sendEmailWithAttachment(Context context) { String filename="/scale.csv"; File filelocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), filename); //Uri path = Uri.fromFile(filelocation); Uri path = FileProvider.getUriForFile(context, "your.application.package.fileprovider", filelocation); Intent emailIntent = new Intent(Intent.ACTION_SEND); // set the type to 'email' emailIntent .setType("vnd.android.cursor.dir/email"); String to[] = {"email@gmail.com"}; emailIntent .putExtra(Intent.EXTRA_EMAIL, to); emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Scale Data"); emailIntent.putExtra(Intent.EXTRA_TEXT, "This is the body"); emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // the attachment emailIntent .putExtra(Intent.EXTRA_STREAM, path); context.startActivity(Intent.createChooser(emailIntent, "Send mail..."));}有关从android文档定义文件路径的一些提示
<files-path name="name" path="path" />
表示Context.getFilesDir()
<cache-path name="name" path="path" />
代表getCacheDir()
<external-path name="name" path="path" />
表示Environment.getExternalStorageDirectory()。
<external-cache-path name="name" path="path" />
表示Context#getExternalFilesDir(String)Context.getExternalFilesDir(null)
<external-media-path name="name" path="path" />
表示Context.getExternalCacheDir()。
阅读更多文档
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)