我正在开发一个自定义相机应用程序,它可以捕捉图片并将其存储在图库中。当我使用 Intent.ACTION_SEND 分享该图像时,它在所有设备上都运行良好,但具有 API 26,即 OREO 的设备除外。
我分享图片的代码是:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/jpeg");
Uri uriShare = Uri.fromFile(outFile);
//outfile is the path of the image stored in the gallery
shareIntent.putExtra(Intent.EXTRA_STREAM, uriShare);
startActivity(Intent.createChooser(shareIntent, ""));
谁能帮我解决这个问题?
如果targetSdkVersion
高于24,那么FileProvider用于授予访问权限。
创建一个xml文件(路径:res\xml
)provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<root-path name="external_files" path="/" />
</paths>
在 AndroidManifest.xml 中添加一个 Provider在
下
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
和替换
Uri uri = Uri.fromFile(fileImagePath);
到
Uri uri = FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID + ".provider",fileImagePath);
你就完成了。