1.获取下载的路径
File file = new File(getCurrentActivity().getFilesDir(), "");
File newFile = new File(file, nickName);
2.把下载的路径转到fileProvider准备分享
Uri contentUri = FileProvider.getUriForFile(this, "包名.fileProvider", newFile);
3.设置分享属性,例如PDF格式
Intent intent = new Intent(Intent.ACTION_VIEW).setDataAndType(contentUri, "application/pdf");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent)
//打开应用系统文件管理
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
getCurrentActivity().startActivityForResult(intent, 10);
//分享文本或者链接
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, FILE_NAME);;
intent.setType("text/plain");
this.startActivity(Intent.createChooser(intent, "meihao"));
FileProvider环境配置
注意:file_paths.xml配置内容要进行以下内容对比(下载时创建的本地路径要与下进行对应):
<files-path name="name" path="path" /> 物理路径相当于Context.getFilesDir() + /path/
<cache-path name="name" path="path" /> 物理路径相当于Context.getCacheDir() + /path/
<external-path name="name" path="path" /> 物理路径相当于Environment.getExternalStorageDirectory() + /path/
<external-files-path name="name" path="path" /> 物理路径相当于Context.getExternalFilesDir(String) + /path/
<external-cache-path name="name" path="path" /> 物理路径相当于Context.getExternalCacheDir() + /path/
参考:https://blog.csdn.net/chxc_yy/article/details/81536875
开始配置:
1.在Androidstudio /android/app/src/res/xml创建file_paths.xml文件
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="sharedata" path="path"/>
<root-path name="root_path" path="."/>
<external-path name="name" path="path" />
</paths>
在AndroidManifest.xml配置
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="包名.fileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>